Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/features/ban-buttons/ban-command-buttons.ts
Original file line number Diff line number Diff line change
@@ -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<string, BanCommandAction>;

export type BanCommandButtons = {
buildButtons: (
targetId: string
) => ActionRowBuilder<MessageActionRowComponentBuilder>;
};

const canUseBanCommandButtons = (interaction: ButtonInteraction): boolean => {
return isModerator(getCommandUser(interaction));
};
Comment thread
michal-skraburski marked this conversation as resolved.
Resolved

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<MessageActionRowComponentBuilder>();
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;
},
};
};
14 changes: 14 additions & 0 deletions src/features/ban-buttons/repel-ban-buttons.ts
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 13 additions & 0 deletions src/features/ban-buttons/spam-ban-buttons.ts
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 7 additions & 3 deletions src/features/moderation/repel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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)],
},
},
});
};
Expand Down
19 changes: 9 additions & 10 deletions src/features/spam-detection/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -32,17 +33,13 @@ export type LogFunction<T = Rule> = (
export const createLogTextContent = <T extends Rule>(
options: LogFunctionOptions<T>
) => {
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': {
Expand Down Expand Up @@ -152,6 +149,8 @@ export const createLogTextContent = <T extends Rule>(
};

export const defaultLogFunction: LogFunction = async (options) => {
const author = options.messages[0].author;

if (!options.logChannel?.isSendable()) {
return;
}
Expand All @@ -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],
});
};
Loading