From 3e297178c7f6ed3134c330a879deaedad8310b89 Mon Sep 17 00:00:00 2001 From: Ske Date: Wed, 24 Jun 2020 16:47:34 +0200 Subject: [PATCH] Hopefully fix user cache error in ReactionAdd --- PluralKit.Bot/Handlers/ReactionAdded.cs | 5 +++++ PluralKit.Bot/Utils/DiscordUtils.cs | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/PluralKit.Bot/Handlers/ReactionAdded.cs b/PluralKit.Bot/Handlers/ReactionAdded.cs index 48ebc1f1..a7fde738 100644 --- a/PluralKit.Bot/Handlers/ReactionAdded.cs +++ b/PluralKit.Bot/Handlers/ReactionAdded.cs @@ -29,6 +29,11 @@ namespace PluralKit.Bot { // Only proxies in guild text channels if (evt.Channel.Type != ChannelType.Text) return; + + // Sometimes we get events from users that aren't in the user cache + // In that case we get a "broken" user object (where eg. calling IsBot throws an exception) + // We just ignore all of those for now, should be quite rare... + if (!evt.Client.TryGetCachedUser(evt.User.Id, out _)) return; // Ignore reactions from bots (we can't DM them anyway) if (evt.User.IsBot) return; diff --git a/PluralKit.Bot/Utils/DiscordUtils.cs b/PluralKit.Bot/Utils/DiscordUtils.cs index 03fcbf2d..bd493abd 100644 --- a/PluralKit.Bot/Utils/DiscordUtils.cs +++ b/PluralKit.Bot/Utils/DiscordUtils.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -114,5 +115,15 @@ namespace PluralKit.Bot // It's just here for consistency so we don't use the standard SendMessageAsync method >.> public static Task SendMessageFixedAsync(this DiscordMember member, string content = null, DiscordEmbed embed = null) => member.SendMessageAsync(content, embed: embed); + + public static bool TryGetCachedUser(this DiscordClient client, ulong id, out DiscordUser user) + { + user = null; + + var cache = (ConcurrentDictionary) typeof(BaseDiscordClient) + .GetProperty("UserCache", BindingFlags.Instance | BindingFlags.NonPublic) + ?.GetValue(client); + return cache != null && cache.TryGetValue(id, out user); + } } } \ No newline at end of file