Skip to content

Commit

Permalink
Add moderator roles to config
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Mar 22, 2024
1 parent 03fabe1 commit c586ae2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface BotContext {
};

roles: Record<RemoveSuffix<ConfigRoleId, "_role_id">, Role>;
moderatorRoles: Set<Snowflake>;

// 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)
Expand Down Expand Up @@ -77,8 +78,6 @@ export interface BotContext {
// TODO: Add some user assertions like isMod and isTrusted
}

// #region Ensure Channels

function ensureRole<T extends ConfigRoleId>(
config: Config,
guild: Guild,
Expand All @@ -94,6 +93,18 @@ function ensureRole<T extends ConfigRoleId>(

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<T extends ConfigTextChannelId>(
config: Config,
guild: Guild,
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 1 addition & 3 deletions src/handler/cmdHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit c586ae2

Please sign in to comment.