From 720755844fbcb79e1c1aeda9b65d4b4befd84858 Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 2 May 2020 15:29:36 +0200 Subject: [PATCH] Rename BotHasPermission to a more descriptive name --- PluralKit.Bot/Bot.cs | 2 +- PluralKit.Bot/CommandSystem/Context.cs | 4 ++-- PluralKit.Bot/Handlers/MessageCreated.cs | 2 +- PluralKit.Bot/Services/LogChannelService.cs | 3 +-- PluralKit.Bot/Services/LoggerCleanService.cs | 2 +- PluralKit.Bot/Services/ProxyService.cs | 4 ++-- PluralKit.Bot/Utils/ContextUtils.cs | 8 ++++---- PluralKit.Bot/Utils/DiscordUtils.cs | 2 +- 8 files changed, 13 insertions(+), 14 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index c28a2c23..05e24126 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -98,7 +98,7 @@ namespace PluralKit.Bot // Once we've sent it to Sentry, report it to the user (if we have permission to) var reportChannel = handler.ErrorChannelFor(evt); - if (reportChannel != null && reportChannel.BotHasPermission(Permissions.SendMessages)) + if (reportChannel != null && reportChannel.BotHasAllPermissions(Permissions.SendMessages)) { var eid = sentryEvent.EventId; await reportChannel.SendMessageAsync( diff --git a/PluralKit.Bot/CommandSystem/Context.cs b/PluralKit.Bot/CommandSystem/Context.cs index c4f7bae2..a37bd869 100644 --- a/PluralKit.Bot/CommandSystem/Context.cs +++ b/PluralKit.Bot/CommandSystem/Context.cs @@ -64,11 +64,11 @@ namespace PluralKit.Bot public Task Reply(string text = null, DiscordEmbed embed = null) { - if (!this.BotHasPermission(Permissions.SendMessages)) + if (!this.BotHasAllPermissions(Permissions.SendMessages)) // Will be "swallowed" during the error handler anyway, this message is never shown. throw new PKError("PluralKit does not have permission to send messages in this channel."); - if (embed != null && !this.BotHasPermission(Permissions.EmbedLinks)) + if (embed != null && !this.BotHasAllPermissions(Permissions.EmbedLinks)) throw new PKError("PluralKit does not have permission to send embeds in this channel. Please ensure I have the **Embed Links** permission enabled."); return Channel.SendMessageAsync(text, embed: embed); diff --git a/PluralKit.Bot/Handlers/MessageCreated.cs b/PluralKit.Bot/Handlers/MessageCreated.cs index df74bb1e..42bedd41 100644 --- a/PluralKit.Bot/Handlers/MessageCreated.cs +++ b/PluralKit.Bot/Handlers/MessageCreated.cs @@ -118,7 +118,7 @@ namespace PluralKit.Bot catch (PKError e) { // User-facing errors, print to the channel properly formatted - if (msg.Channel.Guild == null || msg.Channel.BotHasPermission(Permissions.SendMessages)) + if (msg.Channel.Guild == null || msg.Channel.BotHasAllPermissions(Permissions.SendMessages)) await msg.Channel.SendMessageAsync($"{Emojis.Error} {e.Message}"); } diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 82c30e29..b87f19e8 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -47,8 +47,7 @@ namespace PluralKit.Bot { if (channel.Type != ChannelType.Text) return; // Bail if we don't have permission to send stuff here - var neededPermissions = Permissions.SendMessages | Permissions.EmbedLinks; - if ((channel.BotPermissions() & neededPermissions) != neededPermissions) + if (!channel.BotHasAllPermissions(Permissions.SendMessages | Permissions.EmbedLinks)) return; var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, originalMsgId, sender, content, originalChannel); diff --git a/PluralKit.Bot/Services/LoggerCleanService.cs b/PluralKit.Bot/Services/LoggerCleanService.cs index ce71bd19..3d7374a2 100644 --- a/PluralKit.Bot/Services/LoggerCleanService.cs +++ b/PluralKit.Bot/Services/LoggerCleanService.cs @@ -67,7 +67,7 @@ namespace PluralKit.Bot // Bail if not enabled, or if we don't have permission here if (!cachedGuild.LogCleanupEnabled) return; if (msg.Channel.Type != ChannelType.Text) return; - if (!msg.Channel.BotHasPermission(Permissions.ManageMessages)) return; + if (!msg.Channel.BotHasAllPermissions(Permissions.ManageMessages)) return; // If this message is from a *webhook*, check if the name matches one of the bots we know // TODO: do we need to do a deeper webhook origin check, or would that be too hard on the rate limit? diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index 22c7ed91..f56b6a51 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -287,7 +287,7 @@ namespace PluralKit.Bot await args.Channel.SendMessageAsync($"Psst, **{msg.Member.DisplayName ?? msg.Member.Name}** (<@{msg.Message.Sender}>), you have been pinged by <@{args.User.Id}>.", embed: embed.Build()); // Finally remove the original reaction (if we can) - if (args.Channel.BotHasPermission(Permissions.ManageMessages)) + if (args.Channel.BotHasAllPermissions(Permissions.ManageMessages)) await args.Message.DeleteReactionAsync(args.Emoji, args.User); } @@ -323,7 +323,7 @@ namespace PluralKit.Bot public async Task HandleMessageDeletionByReaction(MessageReactionAddEventArgs args) { // Bail if we don't have permission to delete - if (!args.Channel.BotHasPermission(Permissions.ManageMessages)) return; + if (!args.Channel.BotHasAllPermissions(Permissions.ManageMessages)) return; // Find the message in the database var storedMessage = await _data.GetMessage(args.Message.Id); diff --git a/PluralKit.Bot/Utils/ContextUtils.cs b/PluralKit.Bot/Utils/ContextUtils.cs index 9f4631f7..71123bd2 100644 --- a/PluralKit.Bot/Utils/ContextUtils.cs +++ b/PluralKit.Bot/Utils/ContextUtils.cs @@ -109,7 +109,7 @@ namespace PluralKit.Bot { if (currentPage < 0) currentPage += pageCount; // If we can, remove the user's reaction (so they can press again quickly) - if (ctx.BotHasPermission(Permissions.ManageMessages)) await msg.DeleteReactionAsync(reaction.Emoji, reaction.User); + if (ctx.BotHasAllPermissions(Permissions.ManageMessages)) await msg.DeleteReactionAsync(reaction.Emoji, reaction.User); // Edit the embed with the new page var embed = await MakeEmbedForPage(currentPage); @@ -119,7 +119,7 @@ namespace PluralKit.Bot { // "escape hatch", clean up as if we hit X } - if (ctx.BotHasPermission(Permissions.ManageMessages)) await msg.DeleteAllReactionsAsync(); + if (ctx.BotHasAllPermissions(Permissions.ManageMessages)) await msg.DeleteAllReactionsAsync(); } // If we get a "NotFound" error, the message has been deleted and thus not our problem catch (NotFoundException) { } @@ -208,8 +208,8 @@ namespace PluralKit.Bot { public static Permissions BotPermissions(this Context ctx) => ctx.Channel.BotPermissions(); - public static bool BotHasPermission(this Context ctx, Permissions permission) => - ctx.Channel.BotHasPermission(permission); + public static bool BotHasAllPermissions(this Context ctx, Permissions permission) => + ctx.Channel.BotHasAllPermissions(permission); public static async Task BusyIndicator(this Context ctx, Func f, string emoji = "\u23f3" /* hourglass */) { diff --git a/PluralKit.Bot/Utils/DiscordUtils.cs b/PluralKit.Bot/Utils/DiscordUtils.cs index 0598d7d1..3b0a8b88 100644 --- a/PluralKit.Bot/Utils/DiscordUtils.cs +++ b/PluralKit.Bot/Utils/DiscordUtils.cs @@ -58,7 +58,7 @@ namespace PluralKit.Bot return Permissions.None; } - public static bool BotHasPermission(this DiscordChannel channel, Permissions permissionSet) => + public static bool BotHasAllPermissions(this DiscordChannel channel, Permissions permissionSet) => (BotPermissions(channel) & permissionSet) == permissionSet; public static Instant SnowflakeToInstant(ulong snowflake) =>