diff --git a/src/features/ban-buttons/ban-command-buttons.ts b/src/features/ban-buttons/ban-command-buttons.ts new file mode 100644 index 0000000..75fa2e1 --- /dev/null +++ b/src/features/ban-buttons/ban-command-buttons.ts @@ -0,0 +1,106 @@ +import { + ActionRowBuilder, + ButtonBuilder, + ButtonInteraction, + ButtonStyle, + codeBlock, + type MessageActionRowComponentBuilder, + MessageFlags, +} 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'; +import { getCommandUser } from '@/util/member.js'; + +export type BanCommandAction = { + label: string; + command: 'ban' | 'softban'; + duration: string | null; +}; + +export type BanCommandActions = Record; + +export type BanCommandButtons = { + buildButtons: ( + targetId: string + ) => ActionRowBuilder; +}; + +const canUseBanCommandButtons = (interaction: ButtonInteraction): boolean => { + return isModerator(getCommandUser(interaction)); +}; + +const buildBanCommand = ( + targetId: string, + action: BanCommandAction +): 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(' '); +}; + +export const createBanCommandButtons = ({ + id, + actions, +}: { + id: string; + actions: BanCommandActions; +}): BanCommandButtons => { + const handler: ButtonSubmitInteraction = { + commandName: id, + handler: async (interaction) => { + if (!canUseBanCommandButtons(interaction)) { + await interaction.reply({ + content: 'Only moderators can use these buttons.', + flags: MessageFlags.Ephemeral, + }); + return; + } + + const [, key, targetId] = parseCustomId(interaction.customId); + const action = key ? actions[key] : undefined; + 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${codeBlock(command)}`, + flags: MessageFlags.Ephemeral, + }); + }, + }; + + registerButtonSubmitInteraction(handler); + + return { + buildButtons: (targetId: string) => { + const row = new ActionRowBuilder(); + for (const [key, action] of Object.entries(actions)) { + row.addComponents( + new ButtonBuilder() + .setCustomId(customId(id, key, targetId)) + .setLabel(action.label) + .setStyle( + action.command === 'softban' + ? ButtonStyle.Secondary + : ButtonStyle.Danger + ) + ); + } + return row; + }, + }; +}; diff --git a/src/features/ban-buttons/repel-ban-buttons.ts b/src/features/ban-buttons/repel-ban-buttons.ts new file mode 100644 index 0000000..6d39442 --- /dev/null +++ b/src/features/ban-buttons/repel-ban-buttons.ts @@ -0,0 +1,14 @@ +import { createBanCommandButtons } from './ban-command-buttons.js'; + +const repelBanButtons = createBanCommandButtons({ + id: 'repel-ban', + actions: { + 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 }, + }, +}); + +export const buildRepelBanButtons = (targetId: string) => + repelBanButtons.buildButtons(targetId); diff --git a/src/features/ban-buttons/spam-ban-buttons.ts b/src/features/ban-buttons/spam-ban-buttons.ts new file mode 100644 index 0000000..02bed3d --- /dev/null +++ b/src/features/ban-buttons/spam-ban-buttons.ts @@ -0,0 +1,13 @@ +import { createBanCommandButtons } from './ban-command-buttons.js'; + +const spamBanButtons = createBanCommandButtons({ + id: 'spam-ban', + actions: { + scam: { label: 'Scam', command: 'ban', duration: '30d' }, + advertising: { label: 'Advertising', command: 'ban', duration: '30d' }, + hacked: { label: 'Hacked Account', command: 'softban', duration: null }, + }, +}); + +export const buildSpamBanButtons = (targetId: string) => + spamBanButtons.buildButtons(targetId); diff --git a/src/features/moderation/repel.ts b/src/features/moderation/repel.ts index 8e38878..f091ce5 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 '@/features/ban-buttons/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)], + }, }, }); }; diff --git a/src/features/spam-detection/logs.ts b/src/features/spam-detection/logs.ts index 4cb1b60..a3fffd6 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 '@/features/ban-buttons/spam-ban-buttons.js'; import type { Rule } from './rules-config.js'; const makeLogMessageTitleAndContent = (title: string, content: string) => { @@ -32,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': { @@ -152,6 +149,8 @@ export const createLogTextContent = ( }; export const defaultLogFunction: LogFunction = async (options) => { + const author = options.messages[0].author; + if (!options.logChannel?.isSendable()) { return; } @@ -162,18 +161,18 @@ 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(author.id); + await options.logChannel.send({ flags: MessageFlags.IsComponentsV2, allowedMentions: { users: undefined }, - components: [containerComponent], + components: [containerComponent, banButtons], }); };