diff --git a/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs b/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs index 36762778..47089956 100644 --- a/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs +++ b/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs @@ -51,8 +51,6 @@ internal partial class Validator : IValidateOptions public Shard? Shard { get; set; } - public bool? CacheDMChannels { get; set; } - public RestClientConfiguration? RestClientConfiguration { get; set; } internal GatewayClientConfiguration CreateConfiguration() @@ -71,7 +69,6 @@ internal GatewayClientConfiguration CreateConfiguration() LargeThreshold, Presence, Shard, - CacheDMChannels, RestClientConfiguration); } } diff --git a/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs b/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs index 35476a76..c42f8aec 100644 --- a/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs +++ b/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs @@ -52,8 +52,6 @@ internal partial class Validator : IValidateOptions public int? ShardCount { get; set; } - public bool? CacheDMChannels { get; set; } - public RestClientConfiguration? RestClientConfiguration { get; set; } // Simple properties @@ -98,7 +96,6 @@ internal ShardedGatewayClientConfiguration CreateConfiguration() CreateFactory(LargeThreshold, LargeThresholdFactory), CreateFactory(Presence, PresenceFactory), ShardCount, - CacheDMChannels, RestClientConfiguration); static Func? CreateFactory(T? value, Func? func, [CallerArgumentExpression(nameof(value))] string valueName = "", [CallerArgumentExpression(nameof(func))] string funcName = "") diff --git a/NetCord.Services/ApplicationCommands/ApplicationCommandContexts.cs b/NetCord.Services/ApplicationCommands/ApplicationCommandContexts.cs index 41723cfb..0818bd7e 100644 --- a/NetCord.Services/ApplicationCommands/ApplicationCommandContexts.cs +++ b/NetCord.Services/ApplicationCommands/ApplicationCommandContexts.cs @@ -19,6 +19,8 @@ public class ApplicationCommandContext(ApplicationCommandInteraction interaction public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public User User => Interaction.User; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpApplicationCommandContext(ApplicationCommandInteraction interaction, RestClient client) @@ -50,6 +52,8 @@ public class SlashCommandContext(SlashCommandInteraction interaction, GatewayCli public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public User User => Interaction.User; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpSlashCommandContext(SlashCommandInteraction interaction, RestClient client) @@ -82,6 +86,8 @@ public class UserCommandContext(UserCommandInteraction interaction, GatewayClien public TextChannel Channel => Interaction.Channel; public User User => Interaction.User; public User Target => Interaction.Data.TargetUser; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpUserCommandContext(UserCommandInteraction interaction, RestClient client) @@ -115,6 +121,8 @@ public class MessageCommandContext(MessageCommandInteraction interaction, Gatewa public TextChannel Channel => Interaction.Channel; public User User => Interaction.User; public RestMessage Target => Interaction.Data.TargetMessage; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpMessageCommandContext(MessageCommandInteraction interaction, RestClient client) diff --git a/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs b/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs index 74bb10a8..577d368d 100644 --- a/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs +++ b/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs @@ -18,4 +18,6 @@ public class AutocompleteInteractionContext(AutocompleteInteraction interaction, public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public User User => Interaction.User; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } diff --git a/NetCord.Services/Commands/CommandContexts.cs b/NetCord.Services/Commands/CommandContexts.cs index 036790e0..55ee5787 100644 --- a/NetCord.Services/Commands/CommandContexts.cs +++ b/NetCord.Services/Commands/CommandContexts.cs @@ -18,4 +18,6 @@ public class CommandContext(Message message, GatewayClient client) public Guild? Guild => Message.Guild; public TextChannel? Channel => Message.Channel; public User User => Message.Author; + + ulong? IGuildContext.GuildId => Message.GuildId; } diff --git a/NetCord.Services/ComponentInteractions/ComponentInteractionContexts.cs b/NetCord.Services/ComponentInteractions/ComponentInteractionContexts.cs index 42425545..e5b81222 100644 --- a/NetCord.Services/ComponentInteractions/ComponentInteractionContexts.cs +++ b/NetCord.Services/ComponentInteractions/ComponentInteractionContexts.cs @@ -19,6 +19,8 @@ public class ComponentInteractionContext(ComponentInteraction interaction, Gatew public User User => Interaction.User; public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpComponentInteractionContext(ComponentInteraction interaction, RestClient client) @@ -52,6 +54,8 @@ public class MessageComponentInteractionContext(MessageComponentInteraction inte public User User => Interaction.User; public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpMessageComponentInteractionContext(MessageComponentInteraction interaction, RestClient client) @@ -87,6 +91,8 @@ public class ButtonInteractionContext(ButtonInteraction interaction, GatewayClie public User User => Interaction.User; public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpButtonInteractionContext(ButtonInteraction interaction, RestClient client) @@ -123,6 +129,8 @@ public class StringMenuInteractionContext(StringMenuInteraction interaction, Gat public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList SelectedValues => Interaction.Data.SelectedValues; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpStringMenuInteractionContext(StringMenuInteraction interaction, RestClient client) @@ -160,6 +168,8 @@ public class EntityMenuInteractionContext(EntityMenuInteraction interaction, Gat public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList SelectedValues => Interaction.Data.SelectedValues; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpEntityMenuInteractionContext(EntityMenuInteraction interaction, RestClient client) @@ -197,6 +207,8 @@ public class UserMenuInteractionContext(UserMenuInteraction interaction, Gateway public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList SelectedUsers { get; } = Utils.GetUserMenuValues(interaction); + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpUserMenuInteractionContext(UserMenuInteraction interaction, RestClient client) @@ -234,6 +246,8 @@ public class RoleMenuInteractionContext(RoleMenuInteraction interaction, Gateway public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList SelectedRoles { get; } = Utils.GetRoleMenuValues(interaction); + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpRoleMenuInteractionContext(RoleMenuInteraction interaction, RestClient client) @@ -271,6 +285,8 @@ public class MentionableMenuInteractionContext(MentionableMenuInteraction intera public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList SelectedMentionables { get; } = Utils.GetMentionableMenuValues(interaction); + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpMentionableMenuInteractionContext(MentionableMenuInteraction interaction, RestClient client) @@ -308,6 +324,8 @@ public class ChannelMenuInteractionContext(ChannelMenuInteraction interaction, G public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList SelectedChannels { get; } = Utils.GetChannelMenuValues(interaction); + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpChannelMenuInteractionContext(ChannelMenuInteraction interaction, RestClient client) @@ -343,6 +361,8 @@ public class ModalInteractionContext(ModalInteraction interaction, GatewayClient public Guild? Guild => Interaction.Guild; public TextChannel Channel => Interaction.Channel; public IReadOnlyList Components => Interaction.Data.Components; + + ulong? IGuildContext.GuildId => Interaction.GuildId; } public class HttpModalInteractionContext(ModalInteraction interaction, RestClient client) diff --git a/NetCord.Services/Contexts/IGuildContext.cs b/NetCord.Services/Contexts/IGuildContext.cs index af4b8bd0..54490e23 100644 --- a/NetCord.Services/Contexts/IGuildContext.cs +++ b/NetCord.Services/Contexts/IGuildContext.cs @@ -5,4 +5,6 @@ namespace NetCord.Services; public interface IGuildContext { public Guild? Guild { get; } + + internal protected ulong? GuildId { get; } } diff --git a/NetCord.Services/PreconditionAttributes/RequireContextAttribute.cs b/NetCord.Services/PreconditionAttributes/RequireContextAttribute.cs index c6f3e17e..178fc499 100644 --- a/NetCord.Services/PreconditionAttributes/RequireContextAttribute.cs +++ b/NetCord.Services/PreconditionAttributes/RequireContextAttribute.cs @@ -4,48 +4,55 @@ namespace NetCord.Services; -public class RequireContextAttribute : PreconditionAttribute where TContext : IChannelContext +#pragma warning disable IDE0290 // Use primary constructor + +public class RequireContextAttribute : PreconditionAttribute where TContext : IGuildContext { - public RequiredContext RequiredContext { get; } + public RequiredContext RequiredContext => GetRequiredContext(_guild); public string Format => _format.Format; + private readonly bool _guild; private readonly CompositeFormat _format; /// /// {0} - required context public RequireContextAttribute(RequiredContext requiredContext, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format = "Required context: {0}.") { - if (requiredContext > RequiredContext.DM) - throw new InvalidEnumArgumentException(nameof(requiredContext), (int)requiredContext, typeof(RequiredContext)); + _guild = requiredContext switch + { + RequiredContext.Guild => true, + RequiredContext.DM => false, + _ => throw new InvalidEnumArgumentException(nameof(requiredContext), (int)requiredContext, typeof(RequiredContext)), + }; - RequiredContext = requiredContext; _format = CompositeFormat.Parse(format); } public override ValueTask EnsureCanExecuteAsync(TContext context, IServiceProvider? serviceProvider) { - var channel = context.Channel; - - var requiredContext = RequiredContext; + var guild = _guild; + var hasValue = context.GuildId.HasValue; - if (requiredContext switch + if (guild switch { - RequiredContext.Guild => channel is not IGuildChannel, - RequiredContext.GroupDM => channel is not GroupDMChannel, - RequiredContext.DM => channel is not DMChannel, - _ => throw new InvalidOperationException(), + true => !hasValue, + false => hasValue, }) + { + var requiredContext = GetRequiredContext(guild); return new(new InvalidContextResult(string.Format(null, _format, requiredContext), requiredContext)); + } return new(PreconditionResult.Success); } + + private static RequiredContext GetRequiredContext(bool guild) => guild ? RequiredContext.Guild : RequiredContext.DM; } public enum RequiredContext : byte { Guild, - GroupDM, DM, } diff --git a/NetCord/Gateway/GatewayClient.cs b/NetCord/Gateway/GatewayClient.cs index a0e5016f..f6009180 100644 --- a/NetCord/Gateway/GatewayClient.cs +++ b/NetCord/Gateway/GatewayClient.cs @@ -17,9 +17,6 @@ public partial class GatewayClient : WebSocketClient, IEntity private readonly int? _largeThreshold; private readonly PresenceProperties? _presence; private readonly GatewayIntents _intents; - private readonly bool _cacheDMChannels; - private readonly object? _DMsLock; - private readonly Dictionary? _DMSemaphores; private readonly IGatewayCompression _compression; private readonly bool _disposeRest; @@ -837,12 +834,6 @@ public partial class GatewayClient : WebSocketClient, IEntity _presence = configuration.Presence; _intents = configuration.Intents.GetValueOrDefault(GatewayIntents.AllNonPrivileged); - if (_cacheDMChannels = configuration.CacheDMChannels.GetValueOrDefault(true)) - { - _DMsLock = new(); - _DMSemaphores = []; - } - var compression = _compression = configuration.Compression ?? IGatewayCompression.CreateDefault(); Uri = new($"wss://{configuration.Hostname ?? Discord.GatewayHostname}/?v={(int)configuration.Version.GetValueOrDefault(ApiVersion.V10)}&encoding=json&compress={compression.Name}", UriKind.Absolute); Cache = configuration.Cache ?? new GatewayClientCache(); @@ -1290,36 +1281,12 @@ await InvokeEventAsync(Ready, args, data => break; case "MESSAGE_CREATE": { - await InvokeEventAsync( - MessageCreate, - () => data.ToObject(Serialization.Default.JsonMessage), - json => Message.CreateFromJson(json, Cache, Rest), - json => _cacheDMChannels && !json.GuildId.HasValue && !json.Flags.GetValueOrDefault().HasFlag(MessageFlags.Ephemeral), - json => - { - var channelId = json.ChannelId; - if (!_DMSemaphores!.TryGetValue(channelId, out var semaphore)) - _DMSemaphores.Add(channelId, semaphore = new(1, 1)); - return semaphore; - }, - json => CacheChannelAsync(json.ChannelId)).ConfigureAwait(false); + await InvokeEventAsync(MessageCreate, () => Message.CreateFromJson(data.ToObject(Serialization.Default.JsonMessage), Cache, Rest)).ConfigureAwait(false); } break; case "MESSAGE_UPDATE": { - await InvokeEventAsync( - MessageUpdate, - () => data.ToObject(Serialization.Default.JsonMessage), - json => Message.CreateFromJson(json, Cache, Rest), - json => _cacheDMChannels && !json.GuildId.HasValue && !json.Flags.GetValueOrDefault().HasFlag(MessageFlags.Ephemeral), - json => - { - var channelId = json.ChannelId; - if (!_DMSemaphores!.TryGetValue(channelId, out var semaphore)) - _DMSemaphores.Add(channelId, semaphore = new(1, 1)); - return semaphore; - }, - json => CacheChannelAsync(json.ChannelId)).ConfigureAwait(false); + await InvokeEventAsync(MessageUpdate, () => Message.CreateFromJson(data.ToObject(Serialization.Default.JsonMessage), Cache, Rest)).ConfigureAwait(false); } break; case "MESSAGE_DELETE": @@ -1457,20 +1424,6 @@ await InvokeEventAsync( [MethodImpl(MethodImplOptions.AggressiveInlining)] ulong GetGuildId() => data.GetProperty("guild_id").ToObject(Serialization.Default.UInt64); - - async ValueTask CacheChannelAsync(ulong channelId) - { - var cache = Cache; - if (!cache.DMChannels.ContainsKey(channelId)) - { - var channel = await Rest.GetChannelAsync(channelId).ConfigureAwait(false); - if (channel is DMChannel dMChannel) - { - lock (_DMsLock!) - Cache = Cache.CacheDMChannel(dMChannel); - } - } - } } protected override void Dispose(bool disposing) diff --git a/NetCord/Gateway/GatewayClientCache.cs b/NetCord/Gateway/GatewayClientCache.cs index 0fd8c32e..a1acdb04 100644 --- a/NetCord/Gateway/GatewayClientCache.cs +++ b/NetCord/Gateway/GatewayClientCache.cs @@ -10,7 +10,6 @@ public sealed record GatewayClientCache : IGatewayClientCache { public GatewayClientCache() { - _DMChannels = CollectionsUtils.CreateImmutableDictionary(); _guilds = CollectionsUtils.CreateImmutableDictionary(); } @@ -19,18 +18,15 @@ public GatewayClientCache(JsonGatewayClientCache jsonModel, ulong clientId, Rest var userModel = jsonModel.User; if (userModel is not null) _user = new(userModel, client); - _DMChannels = jsonModel.DMChannels.ToImmutableDictionary(c => c.Id, c => DMChannel.CreateFromJson(c, client)); _guilds = jsonModel.Guilds.ToImmutableDictionary(g => g.Id, g => new Guild(g, clientId, client)); } public CurrentUser? User => _user; - public IReadOnlyDictionary DMChannels => _DMChannels; public IReadOnlyDictionary Guilds => _guilds; #pragma warning disable IDE0032 // Use auto property private CurrentUser? _user; #pragma warning restore IDE0032 // Use auto property - private ImmutableDictionary _DMChannels; private ImmutableDictionary _guilds; public JsonGatewayClientCache ToJsonModel() @@ -38,19 +34,10 @@ public JsonGatewayClientCache ToJsonModel() return new() { User = _user is null ? null : ((IJsonModel)_user).JsonModel, - DMChannels = _DMChannels.Select(p => ((IJsonModel)p.Value).JsonModel).ToArray(), Guilds = _guilds.Select(p => ((IJsonModel)p.Value).JsonModel).ToArray(), }; } - public IGatewayClientCache CacheDMChannel(DMChannel dMChannel) - { - return this with - { - _DMChannels = _DMChannels.SetItem(dMChannel.Id, dMChannel), - }; - } - public IGatewayClientCache CacheGuild(Guild guild) { return this with diff --git a/NetCord/Gateway/GatewayClientConfiguration.cs b/NetCord/Gateway/GatewayClientConfiguration.cs index 9b1f6eb9..12f2911e 100644 --- a/NetCord/Gateway/GatewayClientConfiguration.cs +++ b/NetCord/Gateway/GatewayClientConfiguration.cs @@ -24,7 +24,6 @@ public class GatewayClientConfiguration : IWebSocketClientConfiguration public int? LargeThreshold { get; init; } public PresenceProperties? Presence { get; init; } public Shard? Shard { get; init; } - public bool? CacheDMChannels { get; init; } public RestClientConfiguration? RestClientConfiguration { get; init; } IRateLimiterProvider? IWebSocketClientConfiguration.RateLimiterProvider => RateLimiterProvider is { } rateLimiter ? rateLimiter : new GatewayRateLimiterProvider(120, 60_000); diff --git a/NetCord/Gateway/GatewayClientConfigurationFactory.cs b/NetCord/Gateway/GatewayClientConfigurationFactory.cs index 9e14db2a..e17c1b36 100644 --- a/NetCord/Gateway/GatewayClientConfigurationFactory.cs +++ b/NetCord/Gateway/GatewayClientConfigurationFactory.cs @@ -22,7 +22,6 @@ public static GatewayClientConfiguration Create(IWebSocketConnectionProvider? we int? largeThreshold, PresenceProperties? presence, Shard? shard, - bool? cacheDMChannels, RestClientConfiguration? restClientConfiguration) { return new() @@ -41,7 +40,6 @@ public static GatewayClientConfiguration Create(IWebSocketConnectionProvider? we LargeThreshold = largeThreshold, Presence = presence, Shard = shard, - CacheDMChannels = cacheDMChannels, RestClientConfiguration = restClientConfiguration, }; } diff --git a/NetCord/Gateway/IGatewayClientCache.cs b/NetCord/Gateway/IGatewayClientCache.cs index 4b866ff3..3aca69e8 100644 --- a/NetCord/Gateway/IGatewayClientCache.cs +++ b/NetCord/Gateway/IGatewayClientCache.cs @@ -5,10 +5,8 @@ namespace NetCord.Gateway; public interface IGatewayClientCache : IDisposable { public CurrentUser? User { get; } - public IReadOnlyDictionary DMChannels { get; } public IReadOnlyDictionary Guilds { get; } - public IGatewayClientCache CacheDMChannel(DMChannel dMChannel); public IGatewayClientCache CacheGuild(Guild guild); public IGatewayClientCache CacheGuildUser(GuildUser user); public IGatewayClientCache CacheGuildUsers(ulong guildId, IEnumerable users); diff --git a/NetCord/Gateway/JsonModels/JsonGatewayClientCache.cs b/NetCord/Gateway/JsonModels/JsonGatewayClientCache.cs index b3a16cbf..a3e042d6 100644 --- a/NetCord/Gateway/JsonModels/JsonGatewayClientCache.cs +++ b/NetCord/Gateway/JsonModels/JsonGatewayClientCache.cs @@ -9,9 +9,6 @@ public class JsonGatewayClientCache [JsonPropertyName("user")] public JsonUser? User { get; set; } - [JsonPropertyName("dm_channels")] - public IReadOnlyList DMChannels { get; set; } - [JsonPropertyName("guilds")] public IReadOnlyList Guilds { get; set; } } diff --git a/NetCord/Gateway/Message.cs b/NetCord/Gateway/Message.cs index 6006a3a7..68c019b3 100644 --- a/NetCord/Gateway/Message.cs +++ b/NetCord/Gateway/Message.cs @@ -14,7 +14,7 @@ public static Message CreateFromJson(JsonMessage jsonModel, IGatewayClientCache return new(jsonModel, guild, channel, client); } - internal static (Guild?, TextChannel?) GetCacheData(JsonMessage jsonModel, IGatewayClientCache cache) + private static (Guild?, TextChannel?) GetCacheData(JsonMessage jsonModel, IGatewayClientCache cache) { Guild? guild; TextChannel? channel; @@ -37,7 +37,7 @@ internal static (Guild?, TextChannel?) GetCacheData(JsonMessage jsonModel, IGate else { guild = null; - channel = cache.DMChannels.GetValueOrDefault(jsonModel.ChannelId); + channel = null; } return (guild, channel); diff --git a/NetCord/Gateway/ShardedGatewayClient.cs b/NetCord/Gateway/ShardedGatewayClient.cs index d78e68ce..557dbc19 100644 --- a/NetCord/Gateway/ShardedGatewayClient.cs +++ b/NetCord/Gateway/ShardedGatewayClient.cs @@ -43,7 +43,6 @@ private static ShardedGatewayClientConfiguration CreateConfiguration(ShardedGate _ => null, _ => null, null, - null, null); } @@ -61,7 +60,6 @@ private static ShardedGatewayClientConfiguration CreateConfiguration(ShardedGate configuration.LargeThresholdFactory ?? (_ => null), configuration.PresenceFactory ?? (_ => null), configuration.ShardCount, - configuration.CacheDMChannels, configuration.RestClientConfiguration); } @@ -230,7 +228,6 @@ private GatewayClientConfiguration GetGatewayClientConfiguration(Shard shard) configuration.LargeThresholdFactory!(shard), configuration.PresenceFactory!(shard), shard, - configuration.CacheDMChannels, null); } @@ -1604,20 +1601,20 @@ private void UnhookEvent(object @lock, Func? val } } - private static void DisposeClients(IReadOnlyList? clients, bool initialized) + private static void DisposeClients(GatewayClient[]? clients, bool initialized) { if (clients is null) return; - var count = clients.Count; + var length = clients.Length; if (initialized) { - for (int i = 0; i < count; i++) + for (int i = 0; i < length; i++) clients[i]!.Dispose(); } else { - for (int i = 0; i < count; i++) + for (int i = 0; i < length; i++) { var client = clients[i]; if (client is null) diff --git a/NetCord/Gateway/ShardedGatewayClientConfiguration.cs b/NetCord/Gateway/ShardedGatewayClientConfiguration.cs index bab70afa..8f573904 100644 --- a/NetCord/Gateway/ShardedGatewayClientConfiguration.cs +++ b/NetCord/Gateway/ShardedGatewayClientConfiguration.cs @@ -21,6 +21,5 @@ public class ShardedGatewayClientConfiguration public Func? LargeThresholdFactory { get; init; } public Func? PresenceFactory { get; init; } public int? ShardCount { get; init; } - public bool? CacheDMChannels { get; init; } public Rest.RestClientConfiguration? RestClientConfiguration { get; init; } } diff --git a/NetCord/Gateway/ShardedGatewayClientConfigurationFactory.cs b/NetCord/Gateway/ShardedGatewayClientConfigurationFactory.cs index 029f6ca3..dac6a222 100644 --- a/NetCord/Gateway/ShardedGatewayClientConfigurationFactory.cs +++ b/NetCord/Gateway/ShardedGatewayClientConfigurationFactory.cs @@ -22,7 +22,6 @@ public static ShardedGatewayClientConfiguration Create(Func? largeThresholdFactory, Func? presenceFactory, int? shardCount, - bool? cacheDMChannels, RestClientConfiguration? restClientConfiguration) { return new() @@ -41,7 +40,6 @@ public static ShardedGatewayClientConfiguration Create(Func payload); private protected async void InvokeLog(LogMessage logMessage) - { - var log = Log; - if (log is not null) - { - try - { - ValueTask task; - lock (_eventsLock) - task = log(logMessage); - await task.ConfigureAwait(false); - } - catch - { - } - } - } - - private async void InvokeLogWithoutLock(LogMessage logMessage) { var log = Log; if (log is not null) @@ -708,17 +689,14 @@ private protected ValueTask InvokeEventAsync(Func? @event) if (@event is not null) { ValueTask task; - lock (_eventsLock) + try { - try - { - task = @event(); - } - catch (Exception ex) - { - InvokeLogWithoutLock(LogMessage.Error(ex)); - return default; - } + task = @event(); + } + catch (Exception ex) + { + InvokeLog(LogMessage.Error(ex)); + return default; } return AwaitEventAsync(task); @@ -731,19 +709,17 @@ private protected ValueTask InvokeEventAsync(Func? @event, Func { if (@event is not null) { - ValueTask task; var data = dataFunc(); - lock (_eventsLock) + + ValueTask task; + try { - try - { - task = @event(data); - } - catch (Exception ex) - { - InvokeLogWithoutLock(LogMessage.Error(ex)); - return default; - } + task = @event(data); + } + catch (Exception ex) + { + InvokeLog(LogMessage.Error(ex)); + return default; } return AwaitEventAsync(task); @@ -757,17 +733,14 @@ private protected ValueTask InvokeEventAsync(Func? @event, T da if (@event is not null) { ValueTask task; - lock (_eventsLock) + try { - try - { - task = @event(data); - } - catch (Exception ex) - { - InvokeLogWithoutLock(LogMessage.Error(ex)); - return default; - } + task = @event(data); + } + catch (Exception ex) + { + InvokeLog(LogMessage.Error(ex)); + return default; } return AwaitEventAsync(task); @@ -781,27 +754,23 @@ private protected ValueTask InvokeEventAsync(Func? @event, T da if (@event is not null) { ValueTask task; - lock (_eventsLock) + try { - try - { - task = @event(data); - updateData(data); - } - catch (Exception ex) - { - updateData(data); - InvokeLogWithoutLock(LogMessage.Error(ex)); - return default; - } + task = @event(data); + updateData(data); + } + catch (Exception ex) + { + updateData(data); + InvokeLog(LogMessage.Error(ex)); + return default; } return AwaitEventAsync(task); } else { - lock (_eventsLock) - updateData(data); + updateData(data); return default; } } @@ -810,86 +779,30 @@ private protected ValueTask InvokeEventAsync(Func? @event, Func { if (@event is not null) { - ValueTask task; var data = dataFunc(); - lock (_eventsLock) + + ValueTask task; + try { - try - { - task = @event(data); - updateData(); - } - catch (Exception ex) - { - updateData(); - InvokeLogWithoutLock(LogMessage.Error(ex)); - return default; - } + task = @event(data); + updateData(); + } + catch (Exception ex) + { + updateData(); + InvokeLog(LogMessage.Error(ex)); + return default; } return AwaitEventAsync(task); } else { - lock (_eventsLock) - updateData(); + updateData(); return default; } } - private protected async ValueTask InvokeEventAsync(Func? @event, Func partialDataFunc, Func dataFunc, Func cacheFunc, Func semaphoreFunc, Func cacheAsyncFunc) - { - if (@event is not null) - { - var partialData = partialDataFunc(); - ValueTask task; - if (cacheFunc(partialData)) - { - var semaphore = semaphoreFunc(partialData); - await semaphore.WaitAsync().ConfigureAwait(false); - try - { - await cacheAsyncFunc(partialData).ConfigureAwait(false); - var data = dataFunc(partialData); - lock (_eventsLock) - { - try - { - task = @event(data); - } - catch (Exception ex) - { - InvokeLogWithoutLock(LogMessage.Error(ex)); - return; - } - } - } - finally - { - semaphore.Release(); - } - } - else - { - var data = dataFunc(partialData); - lock (_eventsLock) - { - try - { - task = @event(data); - } - catch (Exception ex) - { - InvokeLogWithoutLock(LogMessage.Error(ex)); - return; - } - } - } - - await AwaitEventAsync(task).ConfigureAwait(false); - } - } - private async ValueTask AwaitEventAsync(ValueTask task) { try