From d6cb2db621fc712cf6de15dd183c551a58bce121 Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 1 Apr 2021 21:58:48 +0100 Subject: [PATCH] Check for reaction permissions before adding reactions --- PluralKit.Bot/Utils/ContextUtils.cs | 6 ++++-- PluralKit.Bot/Utils/DiscordUtils.cs | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/PluralKit.Bot/Utils/ContextUtils.cs b/PluralKit.Bot/Utils/ContextUtils.cs index 9f24d176..71da564c 100644 --- a/PluralKit.Bot/Utils/ContextUtils.cs +++ b/PluralKit.Bot/Utils/ContextUtils.cs @@ -34,6 +34,9 @@ namespace PluralKit.Bot { if (user == null) user = ctx.Author; if (timeout == null) timeout = Duration.FromMinutes(5); + if (!DiscordUtils.HasReactionPermissions(ctx)) + await ctx.Reply($"{Emojis.Note} PluralKit does not have permissions to add reactions in this channel. \nPlease reply with 'yes' to confirm, or 'no' to cancel."); + else // "Fork" the task adding the reactions off so we don't have to wait for them to be finished to start listening for presses await ctx.Rest.CreateReactionsBulk(message, new[] {Emojis.Success, Emojis.Error}); @@ -261,8 +264,7 @@ namespace PluralKit.Bot { var task = f(); // If we don't have permission to add reactions, don't bother, and just await the task normally. - var neededPermissions = PermissionSet.AddReactions | PermissionSet.ReadMessageHistory; - if ((ctx.BotPermissions & neededPermissions) != neededPermissions) return await task; + if (!DiscordUtils.HasReactionPermissions(ctx)) return await task; try { diff --git a/PluralKit.Bot/Utils/DiscordUtils.cs b/PluralKit.Bot/Utils/DiscordUtils.cs index 9be30f4e..bf49b42f 100644 --- a/PluralKit.Bot/Utils/DiscordUtils.cs +++ b/PluralKit.Bot/Utils/DiscordUtils.cs @@ -186,5 +186,11 @@ namespace PluralKit.Bot public static string EventType(this IGatewayEvent evt) => evt.GetType().Name.Replace("Event", ""); + + public static bool HasReactionPermissions(Context ctx) + { + var neededPermissions = PermissionSet.AddReactions | PermissionSet.ReadMessageHistory; + return ((ctx.BotPermissions & neededPermissions) == neededPermissions); + } } }