Add IsBotModerator method

Very similar to a previously existing method that was mistakenly removed.
This commit is contained in:
Noi 2020-07-18 00:31:01 -07:00
parent 608ab37aec
commit 581df48855
2 changed files with 11 additions and 3 deletions

View file

@ -120,8 +120,7 @@ namespace BirthdayBot
var gconf = await GuildConfiguration.LoadAsync(channel.Guild.Id); var gconf = await GuildConfiguration.LoadAsync(channel.Guild.Id);
// Ban check // Ban check
bool isMod = gconf.ModeratorRole.HasValue && author.Roles.Any(r => r.Id == gconf.ModeratorRole.Value); if (!gconf.IsBotModerator(author)) // skip check if user is a moderator
if (!isMod) // skip check if user is a moderator
{ {
if (await gconf.IsUserBlockedAsync(author.Id)) return; // silently ignore if (await gconf.IsUserBlockedAsync(author.Id)) return; // silently ignore
} }

View file

@ -1,7 +1,9 @@
using Npgsql; using Discord.WebSocket;
using Npgsql;
using NpgsqlTypes; using NpgsqlTypes;
using System; using System;
using System.Data.Common; using System.Data.Common;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BirthdayBot.Data namespace BirthdayBot.Data
@ -128,6 +130,13 @@ namespace BirthdayBot.Data
return result != 0; return result != 0;
} }
/// <summary>
/// Checks if the given user can be considered a bot moderator.
/// Checks for either the Manage Guild permission or if the user is within a predetermined role.
/// </summary>
public bool IsBotModerator(SocketGuildUser user)
=> user.GuildPermissions.ManageGuild || (ModeratorRole.HasValue && user.Roles.Any(r => r.Id == ModeratorRole.Value));
#region Database #region Database
public const string BackingTable = "settings"; public const string BackingTable = "settings";
public const string BackingTableBans = "banned_users"; public const string BackingTableBans = "banned_users";