fix: update guild cache on GUILD_UPDATE event

This commit is contained in:
spiral 2022-02-04 14:54:56 -05:00
parent 8a9148cd32
commit 47e501da81
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31

View File

@ -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<ulong, bool> Channels = new();
public ConcurrentDictionary<ulong, bool> Channels { get; init; } = new();
}
}