diff --git a/src/context.ts b/src/context.ts index 50ccd77b..fd96a455 100644 --- a/src/context.ts +++ b/src/context.ts @@ -45,6 +45,7 @@ export interface BotContext { }; roles: Record, Role>; + moderatorRoles: Set; // This type is rather "complex" // That's due to the channel IDs in the config not being named consistent (sometimes ends with _channel_id, sometimes with _id only) @@ -77,8 +78,6 @@ export interface BotContext { // TODO: Add some user assertions like isMod and isTrusted } -// #region Ensure Channels - function ensureRole( config: Config, guild: Guild, @@ -94,6 +93,18 @@ function ensureRole( return role; } + +function ensureRoleByDisplayName(guild: Guild, displayName: string): Role { + const role = guild.roles.cache.find(role => role.name === displayName); + if (!role) + throw new Error( + `Role "${displayName}" not found in guild "${guild.id}"`, + ); + return role; +} + +// #region Ensure Channels + function ensureTextChannel( config: Config, guild: Guild, @@ -195,6 +206,11 @@ export async function createBotContext( woisgang: ensureRole(config, guild, "woisgang_role_id"), winner: ensureRole(config, guild, "winner_role_id"), }, + moderatorRoles: new Set([ + ...config.bot_settings.moderator_roles.map( + name => ensureRoleByDisplayName(guild, name).id, + ), + ]), textChannels: { banned: ensureTextChannel(config, guild, "banned_channel_id"), bot_log: ensureTextChannel(config, guild, "bot_log_channel_id"), diff --git a/src/handler/cmdHandler.ts b/src/handler/cmdHandler.ts index b89e99fe..0e5a4c99 100644 --- a/src/handler/cmdHandler.ts +++ b/src/handler/cmdHandler.ts @@ -105,9 +105,7 @@ export default async function ( if ( isModCommand && - !message.member.roles.cache.some(r => - config.bot_settings.moderator_roles.includes(r.name), - ) + !message.member.roles.cache.some(r => context.moderatorRoles.has(r.id)) ) { log.warn( `User "${message.author.tag}" (${message.author}) tried mod command "${cmdPrefix}${command}" and was denied`,