refactor: move guildMember caching to IDiscordCache
This commit is contained in:
parent
99b81085ed
commit
24ac0725af
@ -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);
|
||||
|
@ -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<Channel?> TryGetChannel(ulong channelId);
|
||||
public Task<Channel?> TryGetDmChannel(ulong userId);
|
||||
public Task<User?> TryGetUser(ulong userId);
|
||||
public Task<GuildMemberPartial?> TryGetSelfMember(ulong guildId);
|
||||
public Task<Role?> TryGetRole(ulong roleId);
|
||||
|
||||
public IAsyncEnumerable<Guild> GetAllGuilds();
|
||||
|
@ -15,6 +15,7 @@ namespace Myriad.Cache
|
||||
private readonly ConcurrentDictionary<ulong, CachedGuild> _guilds = new();
|
||||
private readonly ConcurrentDictionary<ulong, Role> _roles = new();
|
||||
private readonly ConcurrentDictionary<ulong, User> _users = new();
|
||||
private readonly ConcurrentDictionary<ulong, GuildMemberPartial> _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<GuildMemberPartial?> TryGetSelfMember(ulong guildId)
|
||||
{
|
||||
_guildMembers.TryGetValue(guildId, out var guildMember);
|
||||
return Task.FromResult(guildMember);
|
||||
}
|
||||
|
||||
public Task<Role?> TryGetRole(ulong roleId)
|
||||
{
|
||||
_roles.TryGetValue(roleId, out var role);
|
||||
|
@ -29,7 +29,6 @@ namespace PluralKit.Bot
|
||||
{
|
||||
public class Bot
|
||||
{
|
||||
private readonly ConcurrentDictionary<ulong, GuildMemberPartial> _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<TypeOfEvent> 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);
|
||||
|
Loading…
Reference in New Issue
Block a user