Check for reaction permissions before adding reactions

This commit is contained in:
spiral 2021-04-01 21:58:48 +01:00
parent 183f74e0ae
commit d6cb2db621
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 10 additions and 2 deletions

View File

@ -34,6 +34,9 @@ namespace PluralKit.Bot {
if (user == null) user = ctx.Author; if (user == null) user = ctx.Author;
if (timeout == null) timeout = Duration.FromMinutes(5); 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 // "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}); await ctx.Rest.CreateReactionsBulk(message, new[] {Emojis.Success, Emojis.Error});
@ -261,8 +264,7 @@ namespace PluralKit.Bot {
var task = f(); var task = f();
// If we don't have permission to add reactions, don't bother, and just await the task normally. // 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 (!DiscordUtils.HasReactionPermissions(ctx)) return await task;
if ((ctx.BotPermissions & neededPermissions) != neededPermissions) return await task;
try try
{ {

View File

@ -186,5 +186,11 @@ namespace PluralKit.Bot
public static string EventType(this IGatewayEvent evt) => public static string EventType(this IGatewayEvent evt) =>
evt.GetType().Name.Replace("Event", ""); evt.GetType().Name.Replace("Event", "");
public static bool HasReactionPermissions(Context ctx)
{
var neededPermissions = PermissionSet.AddReactions | PermissionSet.ReadMessageHistory;
return ((ctx.BotPermissions & neededPermissions) == neededPermissions);
}
} }
} }