2020-12-22 12:15:26 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Myriad.Gateway;
|
|
|
|
|
|
|
|
|
|
namespace Myriad.Cache
|
|
|
|
|
{
|
|
|
|
|
public static class DiscordCacheExtensions
|
|
|
|
|
{
|
|
|
|
|
public static ValueTask HandleGatewayEvent(this IDiscordCache cache, IGatewayEvent evt)
|
|
|
|
|
{
|
|
|
|
|
switch (evt)
|
|
|
|
|
{
|
|
|
|
|
case GuildCreateEvent gc:
|
|
|
|
|
return cache.SaveGuildCreate(gc);
|
|
|
|
|
case GuildUpdateEvent gu:
|
|
|
|
|
return cache.SaveGuild(gu);
|
|
|
|
|
case GuildDeleteEvent gd:
|
|
|
|
|
return cache.RemoveGuild(gd.Id);
|
|
|
|
|
case ChannelCreateEvent cc:
|
|
|
|
|
return cache.SaveChannel(cc);
|
|
|
|
|
case ChannelUpdateEvent cu:
|
|
|
|
|
return cache.SaveChannel(cu);
|
|
|
|
|
case ChannelDeleteEvent cd:
|
|
|
|
|
return cache.RemoveChannel(cd.Id);
|
|
|
|
|
case GuildRoleCreateEvent grc:
|
|
|
|
|
return cache.SaveRole(grc.GuildId, grc.Role);
|
|
|
|
|
case GuildRoleUpdateEvent gru:
|
|
|
|
|
return cache.SaveRole(gru.GuildId, gru.Role);
|
|
|
|
|
case GuildRoleDeleteEvent grd:
|
|
|
|
|
return cache.RemoveRole(grd.GuildId, grd.RoleId);
|
|
|
|
|
case MessageCreateEvent mc:
|
2020-12-24 13:52:44 +00:00
|
|
|
|
return cache.SaveMessageCreate(mc);
|
2020-12-22 12:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async ValueTask SaveGuildCreate(this IDiscordCache cache, GuildCreateEvent guildCreate)
|
|
|
|
|
{
|
|
|
|
|
await cache.SaveGuild(guildCreate);
|
|
|
|
|
|
|
|
|
|
foreach (var channel in guildCreate.Channels)
|
|
|
|
|
// The channel object does not include GuildId for some reason...
|
|
|
|
|
await cache.SaveChannel(channel with { GuildId = guildCreate.Id });
|
|
|
|
|
|
|
|
|
|
foreach (var member in guildCreate.Members)
|
|
|
|
|
await cache.SaveUser(member.User);
|
|
|
|
|
}
|
2020-12-24 13:52:44 +00:00
|
|
|
|
|
|
|
|
|
private static async ValueTask SaveMessageCreate(this IDiscordCache cache, MessageCreateEvent evt)
|
|
|
|
|
{
|
2021-03-18 10:28:11 +00:00
|
|
|
|
// DM messages don't get Channel Create events first, so we need to save
|
|
|
|
|
// some kind of stub channel object until we get the real one
|
|
|
|
|
if (evt.GuildId == null)
|
|
|
|
|
await cache.SaveDmChannelStub(evt.ChannelId);
|
|
|
|
|
|
2020-12-24 13:52:44 +00:00
|
|
|
|
await cache.SaveUser(evt.Author);
|
|
|
|
|
foreach (var mention in evt.Mentions)
|
|
|
|
|
await cache.SaveUser(mention);
|
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|