From 1a268f64644a4080e8f08a1bcd574e524d5dd741 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 18:21:39 +0000 Subject: [PATCH 01/12] feat: add embed buttons to produce commands on repels --- src/features/interaction-create/index.ts | 12 -- src/features/moderation/repel-ban-buttons.ts | 114 +++++++++++++++++++ src/features/moderation/repel.ts | 10 +- 3 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 src/features/moderation/repel-ban-buttons.ts diff --git a/src/features/interaction-create/index.ts b/src/features/interaction-create/index.ts index eb0ebf1..8c16a03 100644 --- a/src/features/interaction-create/index.ts +++ b/src/features/interaction-create/index.ts @@ -28,33 +28,21 @@ export const interactionCreateEvent = createEvent( return; } if (interaction.isButton()) { - logger.info( - `Received button interaction with custom ID: ${interaction.customId}` - ); await handleButtonInteraction(interaction); return; } if (interaction.isModalSubmit()) { - logger.info( - `Received modal submit interaction with custom ID: ${interaction.customId}` - ); await handleModalInteraction(interaction); return; } if (interaction.isStringSelectMenu()) { - logger.info( - `Received select menu interaction with custom ID: ${interaction.customId}` - ); await handleSelectMenuInteraction(interaction); return; } if (interaction.isAutocomplete()) { - logger.info( - `Received autocomplete interaction with custom ID: ${interaction.commandName}` - ); await handleAutoCompleteInteraction(interaction); return; } diff --git a/src/features/moderation/repel-ban-buttons.ts b/src/features/moderation/repel-ban-buttons.ts new file mode 100644 index 0000000..639e1e0 --- /dev/null +++ b/src/features/moderation/repel-ban-buttons.ts @@ -0,0 +1,114 @@ +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyle, + type GuildMember, + type MessageActionRowComponentBuilder, + MessageFlags, + PermissionFlagsBits, +} from 'discord.js'; +import { + type ButtonSubmitInteraction, + registerButtonSubmitInteraction, +} from '@/common/interactions/button-interaction.js'; +import { customId, parseCustomId } from '@/util/custom-id.js'; +import { isModerator } from '@/util/permissions.js'; + +const REPEL_BAN_BUTTON_ID = 'repel-ban'; + +type RepelBanAction = { + key: string; + label: string; + command: 'ban' | 'softban'; + duration: string | null; +}; + +const REPEL_BAN_ACTIONS: RepelBanAction[] = [ + { + key: 'solicit', + label: 'Solicit/Recruit', + command: 'ban', + duration: '356d', + }, + { key: 'scam', label: 'Scam', command: 'ban', duration: '356d' }, + { key: 'disruptive', label: 'Disruptive', command: 'ban', duration: '30d' }, + { key: 'hacked', label: 'Hacked', command: 'softban', duration: null }, +]; + +export const buildRepelBanButtons = (targetId: string) => { + const row = new ActionRowBuilder(); + for (const { key, label, command } of REPEL_BAN_ACTIONS) { + row.addComponents( + new ButtonBuilder() + .setCustomId(customId(REPEL_BAN_BUTTON_ID, key, targetId)) + .setLabel(label) + .setStyle( + command === 'softban' ? ButtonStyle.Secondary : ButtonStyle.Danger + ) + ); + } + return row; +}; + +const canUseRepelBanButtons = (member: GuildMember): boolean => { + if (member.permissions.has(PermissionFlagsBits.ModerateMembers)) { + return true; + } + // isModerator throws when moderator roles are not configured; treat that as + // "not a moderator" rather than letting the button handler blow up. + try { + return isModerator(member); + } catch { + return false; + } +}; + +const buildBanCommand = ({ + targetId, + action, +}: { + targetId: string; + action: RepelBanAction; +}): string => { + if (action.command === 'softban') { + return `/softban user:${targetId} reason:${action.label}`; + } + const parts = ['/ban', `user:${targetId}`, `reason:${action.label}`]; + if (action.duration) { + parts.push(`duration:${action.duration}`); + } + return parts.join(' '); +}; + +const handleRepelBanButton: ButtonSubmitInteraction = { + commandName: REPEL_BAN_BUTTON_ID, + handler: async (interaction) => { + const member = interaction.member as GuildMember | null; + if (!member || !canUseRepelBanButtons(member)) { + await interaction.reply({ + content: 'Only moderators can use these buttons.', + flags: MessageFlags.Ephemeral, + }); + return; + } + + const [, key, targetId] = parseCustomId(interaction.customId); + const action = REPEL_BAN_ACTIONS.find((option) => option.key === key); + if (!action || !targetId) { + await interaction.reply({ + content: 'Unable to build the ban command.', + flags: MessageFlags.Ephemeral, + }); + return; + } + + const command = buildBanCommand({ targetId, action }); + + await interaction.reply({ + content: `# Copy and run this ban command:\n\`\`\`\n${command}\n\`\`\``, + flags: MessageFlags.Ephemeral, + }); + }, +}; + +registerButtonSubmitInteraction(handleRepelBanButton); diff --git a/src/features/moderation/repel.ts b/src/features/moderation/repel.ts index 8e38878..44f000e 100644 --- a/src/features/moderation/repel.ts +++ b/src/features/moderation/repel.ts @@ -23,6 +23,7 @@ import { getPublicChannels } from '../../util/channel.js'; import { logToChannel } from '../../util/channel-logging.js'; import { SERVER_CHANNELS } from '@/constants/channels.js'; import { createLogger } from '@/util/logger.js'; +import { buildRepelBanButtons } from './repel-ban-buttons.js'; const logger = createLogger('moderation'); @@ -382,9 +383,12 @@ const logRepelAction = async ({ await logToChannel({ channel: SERVER_CHANNELS.repelLogs, content: { - type: 'embed', - embed, - content: mentionText, + type: 'custom', + options: { + embeds: embed, + content: mentionText, + components: [buildRepelBanButtons(target.id)], + }, }, }); }; From 639eb295c9db9859a8d25573ed4463293bfcb7a6 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 18:40:38 +0000 Subject: [PATCH 02/12] feat: append ban buttons to spam-detection --- src/features/moderation/repel-ban-buttons.ts | 133 +++---------------- src/features/spam-detection/ban-buttons.ts | 23 ++++ src/features/spam-detection/logs.ts | 5 +- src/util/ban-command-buttons.ts | 131 ++++++++++++++++++ 4 files changed, 177 insertions(+), 115 deletions(-) create mode 100644 src/features/spam-detection/ban-buttons.ts create mode 100644 src/util/ban-command-buttons.ts diff --git a/src/features/moderation/repel-ban-buttons.ts b/src/features/moderation/repel-ban-buttons.ts index 639e1e0..fca5f78 100644 --- a/src/features/moderation/repel-ban-buttons.ts +++ b/src/features/moderation/repel-ban-buttons.ts @@ -1,114 +1,19 @@ -import { - ActionRowBuilder, - ButtonBuilder, - ButtonStyle, - type GuildMember, - type MessageActionRowComponentBuilder, - MessageFlags, - PermissionFlagsBits, -} from 'discord.js'; -import { - type ButtonSubmitInteraction, - registerButtonSubmitInteraction, -} from '@/common/interactions/button-interaction.js'; -import { customId, parseCustomId } from '@/util/custom-id.js'; -import { isModerator } from '@/util/permissions.js'; - -const REPEL_BAN_BUTTON_ID = 'repel-ban'; - -type RepelBanAction = { - key: string; - label: string; - command: 'ban' | 'softban'; - duration: string | null; -}; - -const REPEL_BAN_ACTIONS: RepelBanAction[] = [ - { - key: 'solicit', - label: 'Solicit/Recruit', - command: 'ban', - duration: '356d', - }, - { key: 'scam', label: 'Scam', command: 'ban', duration: '356d' }, - { key: 'disruptive', label: 'Disruptive', command: 'ban', duration: '30d' }, - { key: 'hacked', label: 'Hacked', command: 'softban', duration: null }, -]; - -export const buildRepelBanButtons = (targetId: string) => { - const row = new ActionRowBuilder(); - for (const { key, label, command } of REPEL_BAN_ACTIONS) { - row.addComponents( - new ButtonBuilder() - .setCustomId(customId(REPEL_BAN_BUTTON_ID, key, targetId)) - .setLabel(label) - .setStyle( - command === 'softban' ? ButtonStyle.Secondary : ButtonStyle.Danger - ) - ); - } - return row; -}; - -const canUseRepelBanButtons = (member: GuildMember): boolean => { - if (member.permissions.has(PermissionFlagsBits.ModerateMembers)) { - return true; - } - // isModerator throws when moderator roles are not configured; treat that as - // "not a moderator" rather than letting the button handler blow up. - try { - return isModerator(member); - } catch { - return false; - } -}; - -const buildBanCommand = ({ - targetId, - action, -}: { - targetId: string; - action: RepelBanAction; -}): string => { - if (action.command === 'softban') { - return `/softban user:${targetId} reason:${action.label}`; - } - const parts = ['/ban', `user:${targetId}`, `reason:${action.label}`]; - if (action.duration) { - parts.push(`duration:${action.duration}`); - } - return parts.join(' '); -}; - -const handleRepelBanButton: ButtonSubmitInteraction = { - commandName: REPEL_BAN_BUTTON_ID, - handler: async (interaction) => { - const member = interaction.member as GuildMember | null; - if (!member || !canUseRepelBanButtons(member)) { - await interaction.reply({ - content: 'Only moderators can use these buttons.', - flags: MessageFlags.Ephemeral, - }); - return; - } - - const [, key, targetId] = parseCustomId(interaction.customId); - const action = REPEL_BAN_ACTIONS.find((option) => option.key === key); - if (!action || !targetId) { - await interaction.reply({ - content: 'Unable to build the ban command.', - flags: MessageFlags.Ephemeral, - }); - return; - } - - const command = buildBanCommand({ targetId, action }); - - await interaction.reply({ - content: `# Copy and run this ban command:\n\`\`\`\n${command}\n\`\`\``, - flags: MessageFlags.Ephemeral, - }); - }, -}; - -registerButtonSubmitInteraction(handleRepelBanButton); +import { createBanCommandButtons } from '@/util/ban-command-buttons.js'; + +const repelBanButtons = createBanCommandButtons({ + id: 'repel-ban', + actions: [ + { + key: 'solicit', + label: 'Solicit/Recruit', + command: 'ban', + duration: '356d', + }, + { key: 'scam', label: 'Scam', command: 'ban', duration: '356d' }, + { key: 'disruptive', label: 'Disruptive', command: 'ban', duration: '30d' }, + { key: 'hacked', label: 'Hacked', command: 'softban', duration: null }, + ], +}); + +export const buildRepelBanButtons = (targetId: string) => + repelBanButtons.buildButtons(targetId); diff --git a/src/features/spam-detection/ban-buttons.ts b/src/features/spam-detection/ban-buttons.ts new file mode 100644 index 0000000..a695fad --- /dev/null +++ b/src/features/spam-detection/ban-buttons.ts @@ -0,0 +1,23 @@ +import { createBanCommandButtons } from '@/util/ban-command-buttons.js'; + +const spamBanButtons = createBanCommandButtons({ + id: 'spam-ban', + actions: [ + { key: 'scam', label: 'Scam', command: 'ban', duration: '356d' }, + { + key: 'advertising', + label: 'Advertising', + command: 'ban', + duration: '30d', + }, + { + key: 'hacked', + label: 'Hacked Account', + command: 'softban', + duration: null, + }, + ], +}); + +export const buildSpamBanButtons = (targetId: string) => + spamBanButtons.buildButtons(targetId); diff --git a/src/features/spam-detection/logs.ts b/src/features/spam-detection/logs.ts index 4cb1b60..d199892 100644 --- a/src/features/spam-detection/logs.ts +++ b/src/features/spam-detection/logs.ts @@ -9,6 +9,7 @@ import { ThumbnailBuilder, } from 'discord.js'; import { timeToString } from '@/constants/time.js'; +import { buildSpamBanButtons } from './ban-buttons.js'; import type { Rule } from './rules-config.js'; const makeLogMessageTitleAndContent = (title: string, content: string) => { @@ -171,9 +172,11 @@ export const defaultLogFunction: LogFunction = async (options) => { .addSectionComponents(sectionComponent) .setAccentColor(Colors.Red); + const banButtons = buildSpamBanButtons(options.messages[0].author.id); + await options.logChannel.send({ flags: MessageFlags.IsComponentsV2, allowedMentions: { users: undefined }, - components: [containerComponent], + components: [containerComponent, banButtons], }); }; diff --git a/src/util/ban-command-buttons.ts b/src/util/ban-command-buttons.ts new file mode 100644 index 0000000..1b0dd87 --- /dev/null +++ b/src/util/ban-command-buttons.ts @@ -0,0 +1,131 @@ +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyle, + type GuildMember, + type MessageActionRowComponentBuilder, + MessageFlags, + PermissionFlagsBits, +} from 'discord.js'; +import { + type ButtonSubmitInteraction, + registerButtonSubmitInteraction, +} from '@/common/interactions/button-interaction.js'; +import { customId, parseCustomId } from '@/util/custom-id.js'; +import { isModerator } from '@/util/permissions.js'; + +export type BanCommandAction = { + // Short key stored in the custom id (has to round-trip through it). + key: string; + // Button label, also used as the ban reason in the generated command. + label: string; + // Dyno command to generate: a timed ban or a softban (message purge). + command: 'ban' | 'softban'; + // Dyno duration syntax (e.g. 30d, 356d). Only applies to 'ban'. + duration: string | null; +}; + +export type BanCommandButtons = { + buildButtons: ( + targetId: string + ) => ActionRowBuilder; +}; + +const canUseBanCommandButtons = (member: GuildMember): boolean => { + if (member.permissions.has(PermissionFlagsBits.ModerateMembers)) { + return true; + } + // isModerator throws when moderator roles are not configured; treat that as + // "not a moderator" rather than letting the button handler blow up. + try { + return isModerator(member); + } catch { + return false; + } +}; + +const buildBanCommand = ( + targetId: string, + action: BanCommandAction +): string => { + // Softban has no duration (it bans then immediately unbans to purge messages). + if (action.command === 'softban') { + return `/softban user:${targetId} reason:${action.label}`; + } + // Duration is placed last so a moderator can quickly adjust it before running. + const parts = ['/ban', `user:${targetId}`, `reason:${action.label}`]; + if (action.duration) { + parts.push(`duration:${action.duration}`); + } + return parts.join(' '); +}; + +/** + * Creates a reusable group of moderator-only "ban suggestion" buttons. + * + * Each group is identified by a unique `id` (the first segment of the button + * custom id) and carries its own set of reason/duration actions, so different + * features can offer different selections. Clicking a button replies, ephemeral + * and moderator-only, with a copyable Dyno ban command pre-filled for the target. + * + * Calling this registers the button handler as a side effect, so invoke it once + * at module load. + */ +export const createBanCommandButtons = ({ + id, + actions, +}: { + id: string; + actions: BanCommandAction[]; +}): BanCommandButtons => { + const handler: ButtonSubmitInteraction = { + commandName: id, + handler: async (interaction) => { + const member = interaction.member as GuildMember | null; + if (!member || !canUseBanCommandButtons(member)) { + await interaction.reply({ + content: 'Only moderators can use these buttons.', + flags: MessageFlags.Ephemeral, + }); + return; + } + + const [, key, targetId] = parseCustomId(interaction.customId); + const action = actions.find((option) => option.key === key); + if (!action || !targetId) { + await interaction.reply({ + content: 'Unable to build the ban command.', + flags: MessageFlags.Ephemeral, + }); + return; + } + + const command = buildBanCommand(targetId, action); + await interaction.reply({ + content: `# Copy and run this ban command:\n\`\`\`\n${command}\n\`\`\``, + flags: MessageFlags.Ephemeral, + }); + }, + }; + + registerButtonSubmitInteraction(handler); + + return { + buildButtons: (targetId: string) => { + const row = new ActionRowBuilder(); + for (const action of actions) { + row.addComponents( + new ButtonBuilder() + .setCustomId(customId(id, action.key, targetId)) + .setLabel(action.label) + .setStyle( + action.command === 'softban' + ? ButtonStyle.Secondary + : ButtonStyle.Danger + ) + ); + } + return row; + }, + }; +}; From 873c80d85ad250c70a09635741b554c5ba7aa01b Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 18:43:11 +0000 Subject: [PATCH 03/12] fix: upgrade the bancommand struct to record --- src/features/moderation/repel-ban-buttons.ts | 17 ++++++----------- src/features/spam-detection/ban-buttons.ts | 20 +++++--------------- src/util/ban-command-buttons.ts | 13 +++++++------ 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/features/moderation/repel-ban-buttons.ts b/src/features/moderation/repel-ban-buttons.ts index fca5f78..5a365d2 100644 --- a/src/features/moderation/repel-ban-buttons.ts +++ b/src/features/moderation/repel-ban-buttons.ts @@ -2,17 +2,12 @@ import { createBanCommandButtons } from '@/util/ban-command-buttons.js'; const repelBanButtons = createBanCommandButtons({ id: 'repel-ban', - actions: [ - { - key: 'solicit', - label: 'Solicit/Recruit', - command: 'ban', - duration: '356d', - }, - { key: 'scam', label: 'Scam', command: 'ban', duration: '356d' }, - { key: 'disruptive', label: 'Disruptive', command: 'ban', duration: '30d' }, - { key: 'hacked', label: 'Hacked', command: 'softban', duration: null }, - ], + actions: { + solicit: { label: 'Solicit/Recruit', command: 'ban', duration: '356d' }, + scam: { label: 'Scam', command: 'ban', duration: '356d' }, + disruptive: { label: 'Disruptive', command: 'ban', duration: '30d' }, + hacked: { label: 'Hacked', command: 'softban', duration: null }, + }, }); export const buildRepelBanButtons = (targetId: string) => diff --git a/src/features/spam-detection/ban-buttons.ts b/src/features/spam-detection/ban-buttons.ts index a695fad..1c24c57 100644 --- a/src/features/spam-detection/ban-buttons.ts +++ b/src/features/spam-detection/ban-buttons.ts @@ -2,21 +2,11 @@ import { createBanCommandButtons } from '@/util/ban-command-buttons.js'; const spamBanButtons = createBanCommandButtons({ id: 'spam-ban', - actions: [ - { key: 'scam', label: 'Scam', command: 'ban', duration: '356d' }, - { - key: 'advertising', - label: 'Advertising', - command: 'ban', - duration: '30d', - }, - { - key: 'hacked', - label: 'Hacked Account', - command: 'softban', - duration: null, - }, - ], + actions: { + scam: { label: 'Scam', command: 'ban', duration: '356d' }, + advertising: { label: 'Advertising', command: 'ban', duration: '30d' }, + hacked: { label: 'Hacked Account', command: 'softban', duration: null }, + }, }); export const buildSpamBanButtons = (targetId: string) => diff --git a/src/util/ban-command-buttons.ts b/src/util/ban-command-buttons.ts index 1b0dd87..3b8aea6 100644 --- a/src/util/ban-command-buttons.ts +++ b/src/util/ban-command-buttons.ts @@ -15,8 +15,6 @@ import { customId, parseCustomId } from '@/util/custom-id.js'; import { isModerator } from '@/util/permissions.js'; export type BanCommandAction = { - // Short key stored in the custom id (has to round-trip through it). - key: string; // Button label, also used as the ban reason in the generated command. label: string; // Dyno command to generate: a timed ban or a softban (message purge). @@ -25,6 +23,9 @@ export type BanCommandAction = { duration: string | null; }; +// Keyed by the short identifier stored in the button custom id. +export type BanCommandActions = Record; + export type BanCommandButtons = { buildButtons: ( targetId: string @@ -76,7 +77,7 @@ export const createBanCommandButtons = ({ actions, }: { id: string; - actions: BanCommandAction[]; + actions: BanCommandActions; }): BanCommandButtons => { const handler: ButtonSubmitInteraction = { commandName: id, @@ -91,7 +92,7 @@ export const createBanCommandButtons = ({ } const [, key, targetId] = parseCustomId(interaction.customId); - const action = actions.find((option) => option.key === key); + const action = key ? actions[key] : undefined; if (!action || !targetId) { await interaction.reply({ content: 'Unable to build the ban command.', @@ -113,10 +114,10 @@ export const createBanCommandButtons = ({ return { buildButtons: (targetId: string) => { const row = new ActionRowBuilder(); - for (const action of actions) { + for (const [key, action] of Object.entries(actions)) { row.addComponents( new ButtonBuilder() - .setCustomId(customId(id, action.key, targetId)) + .setCustomId(customId(id, key, targetId)) .setLabel(action.label) .setStyle( action.command === 'softban' From 19c2147b0b8a1ecdb8ca13573511258116ed5d05 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 18:44:04 +0000 Subject: [PATCH 04/12] fix: drop useless comments --- src/util/ban-command-buttons.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/util/ban-command-buttons.ts b/src/util/ban-command-buttons.ts index 3b8aea6..aa85c8a 100644 --- a/src/util/ban-command-buttons.ts +++ b/src/util/ban-command-buttons.ts @@ -15,15 +15,11 @@ import { customId, parseCustomId } from '@/util/custom-id.js'; import { isModerator } from '@/util/permissions.js'; export type BanCommandAction = { - // Button label, also used as the ban reason in the generated command. label: string; - // Dyno command to generate: a timed ban or a softban (message purge). command: 'ban' | 'softban'; - // Dyno duration syntax (e.g. 30d, 356d). Only applies to 'ban'. duration: string | null; }; -// Keyed by the short identifier stored in the button custom id. export type BanCommandActions = Record; export type BanCommandButtons = { @@ -36,8 +32,6 @@ const canUseBanCommandButtons = (member: GuildMember): boolean => { if (member.permissions.has(PermissionFlagsBits.ModerateMembers)) { return true; } - // isModerator throws when moderator roles are not configured; treat that as - // "not a moderator" rather than letting the button handler blow up. try { return isModerator(member); } catch { @@ -49,11 +43,9 @@ const buildBanCommand = ( targetId: string, action: BanCommandAction ): string => { - // Softban has no duration (it bans then immediately unbans to purge messages). if (action.command === 'softban') { return `/softban user:${targetId} reason:${action.label}`; } - // Duration is placed last so a moderator can quickly adjust it before running. const parts = ['/ban', `user:${targetId}`, `reason:${action.label}`]; if (action.duration) { parts.push(`duration:${action.duration}`); @@ -61,17 +53,6 @@ const buildBanCommand = ( return parts.join(' '); }; -/** - * Creates a reusable group of moderator-only "ban suggestion" buttons. - * - * Each group is identified by a unique `id` (the first segment of the button - * custom id) and carries its own set of reason/duration actions, so different - * features can offer different selections. Clicking a button replies, ephemeral - * and moderator-only, with a copyable Dyno ban command pre-filled for the target. - * - * Calling this registers the button handler as a side effect, so invoke it once - * at module load. - */ export const createBanCommandButtons = ({ id, actions, From 02084330cf76bf1e797c773501ab27115ab0aba7 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 18:45:33 +0000 Subject: [PATCH 05/12] fix: use discordjs codeblock for message --- src/util/ban-command-buttons.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/util/ban-command-buttons.ts b/src/util/ban-command-buttons.ts index aa85c8a..46fa5b3 100644 --- a/src/util/ban-command-buttons.ts +++ b/src/util/ban-command-buttons.ts @@ -2,6 +2,7 @@ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, + codeBlock, type GuildMember, type MessageActionRowComponentBuilder, MessageFlags, @@ -84,7 +85,7 @@ export const createBanCommandButtons = ({ const command = buildBanCommand(targetId, action); await interaction.reply({ - content: `# Copy and run this ban command:\n\`\`\`\n${command}\n\`\`\``, + content: `# Copy and run this ban command:\n${codeBlock(command)}`, flags: MessageFlags.Ephemeral, }); }, From 5b6a78bb9d860112a3d254ff847bf35aa0ab27d2 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 18:59:56 +0000 Subject: [PATCH 06/12] fix: move everything in this PR to features/ban-buttons --- src/{util => features/ban-buttons}/ban-command-buttons.ts | 0 src/features/{moderation => ban-buttons}/repel-ban-buttons.ts | 2 +- .../ban-buttons.ts => ban-buttons/spam-ban-buttons.ts} | 2 +- src/features/moderation/repel.ts | 2 +- src/features/spam-detection/logs.ts | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/{util => features/ban-buttons}/ban-command-buttons.ts (100%) rename src/features/{moderation => ban-buttons}/repel-ban-buttons.ts (86%) rename src/features/{spam-detection/ban-buttons.ts => ban-buttons/spam-ban-buttons.ts} (84%) diff --git a/src/util/ban-command-buttons.ts b/src/features/ban-buttons/ban-command-buttons.ts similarity index 100% rename from src/util/ban-command-buttons.ts rename to src/features/ban-buttons/ban-command-buttons.ts diff --git a/src/features/moderation/repel-ban-buttons.ts b/src/features/ban-buttons/repel-ban-buttons.ts similarity index 86% rename from src/features/moderation/repel-ban-buttons.ts rename to src/features/ban-buttons/repel-ban-buttons.ts index 5a365d2..4e3eb3f 100644 --- a/src/features/moderation/repel-ban-buttons.ts +++ b/src/features/ban-buttons/repel-ban-buttons.ts @@ -1,4 +1,4 @@ -import { createBanCommandButtons } from '@/util/ban-command-buttons.js'; +import { createBanCommandButtons } from './ban-command-buttons.js'; const repelBanButtons = createBanCommandButtons({ id: 'repel-ban', diff --git a/src/features/spam-detection/ban-buttons.ts b/src/features/ban-buttons/spam-ban-buttons.ts similarity index 84% rename from src/features/spam-detection/ban-buttons.ts rename to src/features/ban-buttons/spam-ban-buttons.ts index 1c24c57..dfea767 100644 --- a/src/features/spam-detection/ban-buttons.ts +++ b/src/features/ban-buttons/spam-ban-buttons.ts @@ -1,4 +1,4 @@ -import { createBanCommandButtons } from '@/util/ban-command-buttons.js'; +import { createBanCommandButtons } from './ban-command-buttons.js'; const spamBanButtons = createBanCommandButtons({ id: 'spam-ban', diff --git a/src/features/moderation/repel.ts b/src/features/moderation/repel.ts index 44f000e..f091ce5 100644 --- a/src/features/moderation/repel.ts +++ b/src/features/moderation/repel.ts @@ -23,7 +23,7 @@ import { getPublicChannels } from '../../util/channel.js'; import { logToChannel } from '../../util/channel-logging.js'; import { SERVER_CHANNELS } from '@/constants/channels.js'; import { createLogger } from '@/util/logger.js'; -import { buildRepelBanButtons } from './repel-ban-buttons.js'; +import { buildRepelBanButtons } from '@/features/ban-buttons/repel-ban-buttons.js'; const logger = createLogger('moderation'); diff --git a/src/features/spam-detection/logs.ts b/src/features/spam-detection/logs.ts index d199892..d1ed1ae 100644 --- a/src/features/spam-detection/logs.ts +++ b/src/features/spam-detection/logs.ts @@ -9,7 +9,7 @@ import { ThumbnailBuilder, } from 'discord.js'; import { timeToString } from '@/constants/time.js'; -import { buildSpamBanButtons } from './ban-buttons.js'; +import { buildSpamBanButtons } from '@/features/ban-buttons/spam-ban-buttons.js'; import type { Rule } from './rules-config.js'; const makeLogMessageTitleAndContent = (title: string, content: string) => { From d19262baf09d5ee1dc533f67f510ad063a4240fb Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Wed, 2 Dec 2026 19:00:11 +0000 Subject: [PATCH 07/12] fix: readd the logging in interaction-create --- src/features/interaction-create/index.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/features/interaction-create/index.ts b/src/features/interaction-create/index.ts index 8c16a03..eb0ebf1 100644 --- a/src/features/interaction-create/index.ts +++ b/src/features/interaction-create/index.ts @@ -28,21 +28,33 @@ export const interactionCreateEvent = createEvent( return; } if (interaction.isButton()) { + logger.info( + `Received button interaction with custom ID: ${interaction.customId}` + ); await handleButtonInteraction(interaction); return; } if (interaction.isModalSubmit()) { + logger.info( + `Received modal submit interaction with custom ID: ${interaction.customId}` + ); await handleModalInteraction(interaction); return; } if (interaction.isStringSelectMenu()) { + logger.info( + `Received select menu interaction with custom ID: ${interaction.customId}` + ); await handleSelectMenuInteraction(interaction); return; } if (interaction.isAutocomplete()) { + logger.info( + `Received autocomplete interaction with custom ID: ${interaction.commandName}` + ); await handleAutoCompleteInteraction(interaction); return; } From 3260a5a462a0e480bdfa02e5b1935a70513e61bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Skraburski?= Date: Wed, 2 Dec 2026 19:14:59 +0000 Subject: [PATCH 08/12] Change scam ban duration to 30 days Updated the duration for the scam ban action from 356 days to 30 days. --- src/features/ban-buttons/spam-ban-buttons.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/ban-buttons/spam-ban-buttons.ts b/src/features/ban-buttons/spam-ban-buttons.ts index dfea767..02bed3d 100644 --- a/src/features/ban-buttons/spam-ban-buttons.ts +++ b/src/features/ban-buttons/spam-ban-buttons.ts @@ -3,7 +3,7 @@ import { createBanCommandButtons } from './ban-command-buttons.js'; const spamBanButtons = createBanCommandButtons({ id: 'spam-ban', actions: { - scam: { label: 'Scam', command: 'ban', duration: '356d' }, + scam: { label: 'Scam', command: 'ban', duration: '30d' }, advertising: { label: 'Advertising', command: 'ban', duration: '30d' }, hacked: { label: 'Hacked Account', command: 'softban', duration: null }, }, From c4f2dc9bd46beef82accf5f5d414f91320bb53aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Skraburski?= Date: Wed, 2 Dec 2026 19:15:25 +0000 Subject: [PATCH 09/12] fix: Reduce ban duration for solicit and scam actions Updated ban durations for solicit and scam actions from 356 days to 30 days. --- src/features/ban-buttons/repel-ban-buttons.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/ban-buttons/repel-ban-buttons.ts b/src/features/ban-buttons/repel-ban-buttons.ts index 4e3eb3f..6d39442 100644 --- a/src/features/ban-buttons/repel-ban-buttons.ts +++ b/src/features/ban-buttons/repel-ban-buttons.ts @@ -3,8 +3,8 @@ import { createBanCommandButtons } from './ban-command-buttons.js'; const repelBanButtons = createBanCommandButtons({ id: 'repel-ban', actions: { - solicit: { label: 'Solicit/Recruit', command: 'ban', duration: '356d' }, - scam: { label: 'Scam', command: 'ban', duration: '356d' }, + solicit: { label: 'Solicit/Recruit', command: 'ban', duration: '30d' }, + scam: { label: 'Scam', command: 'ban', duration: '30d' }, disruptive: { label: 'Disruptive', command: 'ban', duration: '30d' }, hacked: { label: 'Hacked', command: 'softban', duration: null }, }, From 443ae29a9c9a03a69ea693548fc64c0fba1b0de9 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Thu, 3 Dec 2026 04:35:00 +0000 Subject: [PATCH 10/12] fix: extract and hoist author out of function arguments --- src/features/spam-detection/logs.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/features/spam-detection/logs.ts b/src/features/spam-detection/logs.ts index d1ed1ae..a3fffd6 100644 --- a/src/features/spam-detection/logs.ts +++ b/src/features/spam-detection/logs.ts @@ -33,17 +33,13 @@ export type LogFunction = ( export const createLogTextContent = ( options: LogFunctionOptions ) => { + const author = options.messages[0].author; const content: string[] = []; content.push( makeLogMessageTitleAndContent('Rule Broken', `${options.reason}\n`) ); - content.push( - makeLogMessageTitleAndContent( - 'User', - `<@${options.messages[0].author.id}>\n` - ) - ); + content.push(makeLogMessageTitleAndContent('User', `<@${author.id}>\n`)); switch (options.rule.type) { case 'contentBased': { @@ -153,6 +149,8 @@ export const createLogTextContent = ( }; export const defaultLogFunction: LogFunction = async (options) => { + const author = options.messages[0].author; + if (!options.logChannel?.isSendable()) { return; } @@ -163,16 +161,14 @@ export const defaultLogFunction: LogFunction = async (options) => { const sectionComponent = new SectionBuilder() .addTextDisplayComponents(textTextDisplayComponent) .setThumbnailAccessory( - new ThumbnailBuilder().setURL( - options.messages[0].author.displayAvatarURL() - ) + new ThumbnailBuilder().setURL(author.displayAvatarURL()) ); const containerComponent = new ContainerBuilder() .addSectionComponents(sectionComponent) .setAccentColor(Colors.Red); - const banButtons = buildSpamBanButtons(options.messages[0].author.id); + const banButtons = buildSpamBanButtons(author.id); await options.logChannel.send({ flags: MessageFlags.IsComponentsV2, From 66add4e1f05016d901643a4e78f93ad533a240e9 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Thu, 3 Dec 2026 04:37:14 +0000 Subject: [PATCH 11/12] fix: possible api type error handled inappropriately (code-review) --- src/features/ban-buttons/ban-command-buttons.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/features/ban-buttons/ban-command-buttons.ts b/src/features/ban-buttons/ban-command-buttons.ts index 46fa5b3..76a68f4 100644 --- a/src/features/ban-buttons/ban-command-buttons.ts +++ b/src/features/ban-buttons/ban-command-buttons.ts @@ -3,7 +3,8 @@ import { ButtonBuilder, ButtonStyle, codeBlock, - type GuildMember, + type ButtonInteraction, + GuildMember, type MessageActionRowComponentBuilder, MessageFlags, PermissionFlagsBits, @@ -29,12 +30,17 @@ export type BanCommandButtons = { ) => ActionRowBuilder; }; -const canUseBanCommandButtons = (member: GuildMember): boolean => { - if (member.permissions.has(PermissionFlagsBits.ModerateMembers)) { +const canUseBanCommandButtons = (interaction: ButtonInteraction): boolean => { + if (interaction.memberPermissions?.has(PermissionFlagsBits.ModerateMembers)) { return true; } + // An uncached member arrives as a raw API object, which has no role cache + // for isModerator to inspect. + if (!(interaction.member instanceof GuildMember)) { + return false; + } try { - return isModerator(member); + return isModerator(interaction.member); } catch { return false; } @@ -64,8 +70,7 @@ export const createBanCommandButtons = ({ const handler: ButtonSubmitInteraction = { commandName: id, handler: async (interaction) => { - const member = interaction.member as GuildMember | null; - if (!member || !canUseBanCommandButtons(member)) { + if (!canUseBanCommandButtons(interaction)) { await interaction.reply({ content: 'Only moderators can use these buttons.', flags: MessageFlags.Ephemeral, From 519a23e5e98f876bbc059dbea6e8401d26999fd4 Mon Sep 17 00:00:00 2001 From: michal-skraburski Date: Thu, 3 Dec 2026 05:04:44 +0000 Subject: [PATCH 12/12] fix: simply isUserAllowedToBan function --- .../ban-buttons/ban-command-buttons.ts | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/features/ban-buttons/ban-command-buttons.ts b/src/features/ban-buttons/ban-command-buttons.ts index 76a68f4..75fa2e1 100644 --- a/src/features/ban-buttons/ban-command-buttons.ts +++ b/src/features/ban-buttons/ban-command-buttons.ts @@ -1,13 +1,11 @@ import { ActionRowBuilder, ButtonBuilder, + ButtonInteraction, ButtonStyle, codeBlock, - type ButtonInteraction, - GuildMember, type MessageActionRowComponentBuilder, MessageFlags, - PermissionFlagsBits, } from 'discord.js'; import { type ButtonSubmitInteraction, @@ -15,6 +13,7 @@ import { } from '@/common/interactions/button-interaction.js'; import { customId, parseCustomId } from '@/util/custom-id.js'; import { isModerator } from '@/util/permissions.js'; +import { getCommandUser } from '@/util/member.js'; export type BanCommandAction = { label: string; @@ -31,19 +30,7 @@ export type BanCommandButtons = { }; const canUseBanCommandButtons = (interaction: ButtonInteraction): boolean => { - if (interaction.memberPermissions?.has(PermissionFlagsBits.ModerateMembers)) { - return true; - } - // An uncached member arrives as a raw API object, which has no role cache - // for isModerator to inspect. - if (!(interaction.member instanceof GuildMember)) { - return false; - } - try { - return isModerator(interaction.member); - } catch { - return false; - } + return isModerator(getCommandUser(interaction)); }; const buildBanCommand = (