From 47e501da81f7215848a5ee943677e2495ea69f99 Mon Sep 17 00:00:00 2001 From: spiral Date: Fri, 4 Feb 2022 14:54:56 -0500 Subject: [PATCH] fix: update guild cache on GUILD_UPDATE event --- Myriad/Cache/MemoryDiscordCache.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Myriad/Cache/MemoryDiscordCache.cs b/Myriad/Cache/MemoryDiscordCache.cs index 6ff9f48a..6a6d704b 100644 --- a/Myriad/Cache/MemoryDiscordCache.cs +++ b/Myriad/Cache/MemoryDiscordCache.cs @@ -15,7 +15,18 @@ public class MemoryDiscordCache: IDiscordCache public ValueTask SaveGuild(Guild guild) { - SaveGuildRaw(guild); + if (!_guilds.ContainsKey(guild.Id)) + { + _guilds[guild.Id] = new CachedGuild(guild); + } + else + { + var channels = _guilds[guild.Id].Channels; + _guilds[guild.Id] = new CachedGuild(guild) + { + Channels = channels, + }; + } foreach (var role in guild.Roles) // Don't call SaveRole because that updates guild state @@ -169,11 +180,8 @@ public class MemoryDiscordCache: IDiscordCache return Task.FromResult(guild.Channels.Keys.Select(c => _channels[c])); } - private CachedGuild SaveGuildRaw(Guild guild) => - _guilds.GetOrAdd(guild.Id, (_, g) => new CachedGuild(g), guild); - private record CachedGuild(Guild Guild) { - public readonly ConcurrentDictionary Channels = new(); + public ConcurrentDictionary Channels { get; init; } = new(); } } \ No newline at end of file