From 2d2e54e31e14510883c95fc55b999413ecc90263 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 5 Dec 2021 08:47:10 +0100 Subject: [PATCH] Bot will now check for permissions when trying to apply punishments --- .../Services/UserPunishService.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs b/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs index efaa60902..be357e140 100644 --- a/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs +++ b/src/NadekoBot/Modules/Administration/Services/UserPunishService.cs @@ -99,6 +99,10 @@ namespace NadekoBot.Modules.Administration.Services public async Task ApplyPunishment(IGuild guild, IGuildUser user, IUser mod, PunishmentAction p, int minutes, ulong? roleId, string reason) { + + if (!await CheckPermission(guild, p)) + return; + switch (p) { case PunishmentAction.Mute: @@ -171,6 +175,40 @@ namespace NadekoBot.Modules.Administration.Services } } + /// + /// Used to prevent the bot from hitting 403's when it needs to + /// apply punishments with insufficient permissions + /// + /// Guild the punishment is applied in + /// Punishment to apply + /// Whether the bot has sufficient permissions + private async Task CheckPermission(IGuild guild, PunishmentAction punish) + { + + var botUser = await guild.GetCurrentUserAsync(); + switch (punish) + { + case PunishmentAction.Mute: + return botUser.GuildPermissions.MuteMembers && botUser.GuildPermissions.ManageRoles; + case PunishmentAction.Kick: + return botUser.GuildPermissions.KickMembers; + case PunishmentAction.Ban: + return botUser.GuildPermissions.BanMembers; + case PunishmentAction.Softban: + return botUser.GuildPermissions.BanMembers; // ban + unban + case PunishmentAction.RemoveRoles: + return botUser.GuildPermissions.ManageRoles; + case PunishmentAction.ChatMute: + return botUser.GuildPermissions.ManageRoles; // adds nadeko-mute role + case PunishmentAction.VoiceMute: + return botUser.GuildPermissions.MuteMembers; + case PunishmentAction.AddRole: + return botUser.GuildPermissions.ManageRoles; + default: + return true; + } + } + public async Task CheckAllWarnExpiresAsync() { using (var uow = _db.GetDbContext())