Repository navigation
feat: add embed buttons to produce commands on repels #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1a268f6
feat: add embed buttons to produce commands on repels
michal-skraburski 639eb29
feat: append ban buttons to spam-detection
michal-skraburski 873c80d
fix: upgrade the bancommand struct to record
michal-skraburski 19c2147
fix: drop useless comments
michal-skraburski 0208433
fix: use discordjs codeblock for message
michal-skraburski 5b6a78b
fix: move everything in this PR to features/ban-buttons
michal-skraburski d19262b
fix: readd the logging in interaction-create
michal-skraburski 3260a5a
Change scam ban duration to 30 days
michal-skraburski c4f2dc9
fix: Reduce ban duration for solicit and scam actions
michal-skraburski 443ae29
fix: extract and hoist author out of function arguments
michal-skraburski 66add4e
fix: possible api type error handled inappropriately (code-review)
michal-skraburski 519a23e
fix: simply isUserAllowedToBan function
michal-skraburski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| }; | ||
|
|
||
| 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; | ||
| }, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.