From 56e4f1c00c81b36e3f8c82286dc34bb07cc99f6e Mon Sep 17 00:00:00 2001 From: acw0 Date: Wed, 22 Jul 2020 04:07:18 -0400 Subject: [PATCH 1/2] Create methods to find guilds and channels in cache --- PluralKit.Bot/Utils/DiscordUtils.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/PluralKit.Bot/Utils/DiscordUtils.cs b/PluralKit.Bot/Utils/DiscordUtils.cs index 124683bc..cfc911ca 100644 --- a/PluralKit.Bot/Utils/DiscordUtils.cs +++ b/PluralKit.Bot/Utils/DiscordUtils.cs @@ -256,6 +256,28 @@ namespace PluralKit.Bot public static Task GetMessage(this DiscordRestClient client, ulong channel, ulong message) => WrapDiscordCall(client.GetMessageAsync(channel, message)); + public static DiscordGuild GetGuild(this DiscordShardedClient client, ulong id) + { + DiscordGuild guild; + foreach (DiscordClient shard in client.ShardClients.Values) + { + shard.Guilds.TryGetValue(id, out guild); + if (guild != null) return guild; + } + return null; + } + + public static async Task GetChannel(this DiscordShardedClient client, ulong id, ulong? guildId = null) + { + // we need to know the channel's guild ID to get the cached guild object, so we grab it from the API + if (guildId == null) { + var guild = await WrapDiscordCall(client.ShardClients.Values.FirstOrDefault().GetChannelAsync(id)); + if (guild != null) guildId = guild.Id; + else return null; // we probably don't have the guild in cache if the API doesn't give it to us + } + return client.GetGuild(guildId.Value).GetChannel(id); + } + private static async Task WrapDiscordCall(Task t) where T: class { From 88c28c18c78336772427f6953fa3662edf11a131 Mon Sep 17 00:00:00 2001 From: acw0 Date: Wed, 22 Jul 2020 04:07:35 -0400 Subject: [PATCH 2/2] Fix pk;msg erroring between shards --- PluralKit.Bot/Services/EmbedService.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index d4372d9f..b027cbfd 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -158,8 +158,7 @@ namespace PluralKit.Bot { public async Task CreateMessageInfoEmbed(DiscordClient client, FullMessage msg) { var ctx = LookupContext.ByNonOwner; - - var channel = await client.GetChannel(msg.Message.Channel); + var channel = await _client.GetChannel(msg.Message.Channel); var serverMsg = channel != null ? await channel.GetMessage(msg.Message.Mid) : null; // Need this whole dance to handle cases where: