From 24ac0725af0212769c623716d4836c2c94e90151 Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 21 Nov 2021 12:06:08 -0500 Subject: [PATCH] refactor: move guildMember caching to IDiscordCache --- Myriad/Cache/DiscordCacheExtensions.cs | 15 +++++++++++++++ Myriad/Cache/IDiscordCache.cs | 2 ++ Myriad/Cache/MemoryDiscordCache.cs | 13 +++++++++++++ PluralKit.Bot/Bot.cs | 18 ++---------------- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Myriad/Cache/DiscordCacheExtensions.cs b/Myriad/Cache/DiscordCacheExtensions.cs index 6b864b27..c5659e4a 100644 --- a/Myriad/Cache/DiscordCacheExtensions.cs +++ b/Myriad/Cache/DiscordCacheExtensions.cs @@ -1,3 +1,4 @@ +using System.Linq; using System.Threading.Tasks; using Myriad.Gateway; @@ -51,6 +52,20 @@ namespace Myriad.Cache return default; } + public static ValueTask TryUpdateSelfMember(this IDiscordCache cache, Shard shard, IGatewayEvent evt) + { + if (evt is GuildCreateEvent gc) + return cache.SaveSelfMember(gc.Id, gc.Members.FirstOrDefault(m => m.User.Id == shard.User?.Id)!); + if (evt is MessageCreateEvent mc && mc.Member != null && mc.Author.Id == shard.User?.Id) + return cache.SaveSelfMember(mc.GuildId!.Value, mc.Member); + if (evt is GuildMemberAddEvent gma && gma.User.Id == shard.User?.Id) + return cache.SaveSelfMember(gma.GuildId, gma); + if (evt is GuildMemberUpdateEvent gmu && gmu.User.Id == shard.User?.Id) + return cache.SaveSelfMember(gmu.GuildId, gmu); + + return default; + } + private static async ValueTask SaveGuildCreate(this IDiscordCache cache, GuildCreateEvent guildCreate) { await cache.SaveGuild(guildCreate); diff --git a/Myriad/Cache/IDiscordCache.cs b/Myriad/Cache/IDiscordCache.cs index 095585c3..ebae23f8 100644 --- a/Myriad/Cache/IDiscordCache.cs +++ b/Myriad/Cache/IDiscordCache.cs @@ -10,6 +10,7 @@ namespace Myriad.Cache public ValueTask SaveGuild(Guild guild); public ValueTask SaveChannel(Channel channel); public ValueTask SaveUser(User user); + public ValueTask SaveSelfMember(ulong guildId, GuildMemberPartial member); public ValueTask SaveRole(ulong guildId, Role role); public ValueTask SaveDmChannelStub(ulong channelId); @@ -22,6 +23,7 @@ namespace Myriad.Cache public Task TryGetChannel(ulong channelId); public Task TryGetDmChannel(ulong userId); public Task TryGetUser(ulong userId); + public Task TryGetSelfMember(ulong guildId); public Task TryGetRole(ulong roleId); public IAsyncEnumerable GetAllGuilds(); diff --git a/Myriad/Cache/MemoryDiscordCache.cs b/Myriad/Cache/MemoryDiscordCache.cs index 4f1e1052..3898a7f2 100644 --- a/Myriad/Cache/MemoryDiscordCache.cs +++ b/Myriad/Cache/MemoryDiscordCache.cs @@ -15,6 +15,7 @@ namespace Myriad.Cache private readonly ConcurrentDictionary _guilds = new(); private readonly ConcurrentDictionary _roles = new(); private readonly ConcurrentDictionary _users = new(); + private readonly ConcurrentDictionary _guildMembers = new(); public ValueTask SaveGuild(Guild guild) { @@ -51,6 +52,12 @@ namespace Myriad.Cache return default; } + public ValueTask SaveSelfMember(ulong guildId, GuildMemberPartial member) + { + _guildMembers[guildId] = member; + return default; + } + public ValueTask SaveRole(ulong guildId, Role role) { _roles[role.Id] = role; @@ -149,6 +156,12 @@ namespace Myriad.Cache return Task.FromResult(user); } + public Task TryGetSelfMember(ulong guildId) + { + _guildMembers.TryGetValue(guildId, out var guildMember); + return Task.FromResult(guildMember); + } + public Task TryGetRole(ulong roleId) { _roles.TryGetValue(roleId, out var role); diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 11d93e98..f084053e 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -29,7 +29,6 @@ namespace PluralKit.Bot { public class Bot { - private readonly ConcurrentDictionary _guildMembers = new(); private readonly Cluster _cluster; private readonly DiscordApiClient _rest; @@ -84,7 +83,7 @@ namespace PluralKit.Bot if (channel.GuildId != null) { - var member = _guildMembers.GetValueOrDefault(channel.GuildId.Value); + var member = await _cache.TryGetSelfMember(channel.GuildId.Value); return await _cache.PermissionsFor(channelId, _cluster.User?.Id ?? default, member); } @@ -93,10 +92,9 @@ namespace PluralKit.Bot private async Task OnEventReceived(Shard shard, IGatewayEvent evt) { + await _cache.TryUpdateSelfMember(shard, evt); await _cache.HandleGatewayEvent(evt); - TryUpdateSelfMember(shard, evt); - // HandleEvent takes a type parameter, automatically inferred by the event type // It will then look up an IEventHandler in the DI container and call that object's handler method // For registering new ones, see Modules.cs @@ -120,18 +118,6 @@ namespace PluralKit.Bot await HandleResumed(shard); } - private void TryUpdateSelfMember(Shard shard, IGatewayEvent evt) - { - if (evt is GuildCreateEvent gc) - _guildMembers[gc.Id] = gc.Members.FirstOrDefault(m => m.User.Id == shard.User?.Id); - if (evt is MessageCreateEvent mc && mc.Member != null && mc.Author.Id == shard.User?.Id) - _guildMembers[mc.GuildId!.Value] = mc.Member; - if (evt is GuildMemberAddEvent gma && gma.User.Id == shard.User?.Id) - _guildMembers[gma.GuildId] = gma; - if (evt is GuildMemberUpdateEvent gmu && gmu.User.Id == shard.User?.Id) - _guildMembers[gmu.GuildId] = gmu; - } - private Task HandleResumed(Shard shard) { return UpdateBotStatus(shard);