-
Notifications
You must be signed in to change notification settings - Fork 1
🌟 feat: add DM notification for repel action #41
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
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 | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,13 +3,16 @@ import { | |||||||||||||||||||||||||||||||||||||||||||
| ApplicationCommandType, | ||||||||||||||||||||||||||||||||||||||||||||
| ChannelType, | ||||||||||||||||||||||||||||||||||||||||||||
| type ChatInputCommandInteraction, | ||||||||||||||||||||||||||||||||||||||||||||
| Colors, | ||||||||||||||||||||||||||||||||||||||||||||
| ContainerBuilder, | ||||||||||||||||||||||||||||||||||||||||||||
| EmbedBuilder, | ||||||||||||||||||||||||||||||||||||||||||||
| GuildMember, | ||||||||||||||||||||||||||||||||||||||||||||
| type Message, | ||||||||||||||||||||||||||||||||||||||||||||
| MessageFlags, | ||||||||||||||||||||||||||||||||||||||||||||
| PermissionFlagsBits, | ||||||||||||||||||||||||||||||||||||||||||||
| type Role, | ||||||||||||||||||||||||||||||||||||||||||||
| type TextChannel, | ||||||||||||||||||||||||||||||||||||||||||||
| TextDisplayBuilder, | ||||||||||||||||||||||||||||||||||||||||||||
| type User, | ||||||||||||||||||||||||||||||||||||||||||||
| } from 'discord.js'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { HOUR, MINUTE, timeToString } from '../../constants/time.js'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -90,6 +93,39 @@ const checkCanRepelTarget = ({ | |||||||||||||||||||||||||||||||||||||||||||
| return { ok: true }; | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const sendReasonToTarget = async ({ | ||||||||||||||||||||||||||||||||||||||||||||
| target, | ||||||||||||||||||||||||||||||||||||||||||||
| reason, | ||||||||||||||||||||||||||||||||||||||||||||
| guildName, | ||||||||||||||||||||||||||||||||||||||||||||
| timeoutDuration, | ||||||||||||||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||||||||||||||
| target: GuildMember | User; | ||||||||||||||||||||||||||||||||||||||||||||
| reason: string; | ||||||||||||||||||||||||||||||||||||||||||||
| guildName: string; | ||||||||||||||||||||||||||||||||||||||||||||
| timeoutDuration?: number; | ||||||||||||||||||||||||||||||||||||||||||||
| }): Promise<boolean> => { | ||||||||||||||||||||||||||||||||||||||||||||
| const parts: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||
| parts.push(`You have been repelled from **${guildName}**.`); | ||||||||||||||||||||||||||||||||||||||||||||
| if (timeoutDuration && timeoutDuration > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
| parts.push(`You have been timed out for ${timeToString(timeoutDuration)}.`); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| parts.push(`**Reason:** ${reason}`); | ||||||||||||||||||||||||||||||||||||||||||||
| const messageContent = parts.join('\n'); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const containerComponent = new ContainerBuilder() | ||||||||||||||||||||||||||||||||||||||||||||
| .setAccentColor(Colors.DarkRed) | ||||||||||||||||||||||||||||||||||||||||||||
| .addTextDisplayComponents(new TextDisplayBuilder().setContent(messageContent)); | ||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||
| await target.send({ | ||||||||||||||||||||||||||||||||||||||||||||
| flags: MessageFlags.IsComponentsV2, | ||||||||||||||||||||||||||||||||||||||||||||
| components: [containerComponent], | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const getTargetFromInteraction = async ( | ||||||||||||||||||||||||||||||||||||||||||||
| interaction: ChatInputCommandInteraction | ||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<GuildMember | User> => { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -215,6 +251,7 @@ const logRepelAction = async ({ | |||||||||||||||||||||||||||||||||||||||||||
| deleteCount, | ||||||||||||||||||||||||||||||||||||||||||||
| reason, | ||||||||||||||||||||||||||||||||||||||||||||
| failedChannels, | ||||||||||||||||||||||||||||||||||||||||||||
| dm, | ||||||||||||||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||||||||||||||
| interaction: ChatInputCommandInteraction; | ||||||||||||||||||||||||||||||||||||||||||||
| member: GuildMember; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -223,6 +260,10 @@ const logRepelAction = async ({ | |||||||||||||||||||||||||||||||||||||||||||
| duration?: number; | ||||||||||||||||||||||||||||||||||||||||||||
| deleteCount: number; | ||||||||||||||||||||||||||||||||||||||||||||
| failedChannels: string[]; | ||||||||||||||||||||||||||||||||||||||||||||
| dm: { | ||||||||||||||||||||||||||||||||||||||||||||
| sent: boolean; | ||||||||||||||||||||||||||||||||||||||||||||
| shouldSend: boolean; | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||||||||
| const channelInfo = | ||||||||||||||||||||||||||||||||||||||||||||
| interaction.channel?.type === ChannelType.GuildVoice | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -276,6 +317,14 @@ const logRepelAction = async ({ | |||||||||||||||||||||||||||||||||||||||||||
| .setColor('Orange') | ||||||||||||||||||||||||||||||||||||||||||||
| .setTimestamp(); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if (dm.shouldSend) { | ||||||||||||||||||||||||||||||||||||||||||||
| resultEmbed.addFields({ | ||||||||||||||||||||||||||||||||||||||||||||
| name: 'DM Sent', | ||||||||||||||||||||||||||||||||||||||||||||
| value: dm.sent ? 'Yes' : 'No', | ||||||||||||||||||||||||||||||||||||||||||||
| inline: true, | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const failedChannelsEmbed = | ||||||||||||||||||||||||||||||||||||||||||||
| failedChannels.length > 0 | ||||||||||||||||||||||||||||||||||||||||||||
| ? new EmbedBuilder() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -312,6 +361,7 @@ const RepelOptions = { | |||||||||||||||||||||||||||||||||||||||||||
| LOOK_BACK: 'look_back', | ||||||||||||||||||||||||||||||||||||||||||||
| TIMEOUT_DURATION: 'timeout_duration', | ||||||||||||||||||||||||||||||||||||||||||||
| MESSAGE_FOR_MODS: 'message_for_mods', | ||||||||||||||||||||||||||||||||||||||||||||
| DM_USER: 'dm_user', | ||||||||||||||||||||||||||||||||||||||||||||
| } as const; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| export const repelCommand = createCommand({ | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -372,6 +422,12 @@ export const repelCommand = createCommand({ | |||||||||||||||||||||||||||||||||||||||||||
| type: ApplicationCommandOptionType.String, | ||||||||||||||||||||||||||||||||||||||||||||
| description: 'Optional message to include for moderators in the log', | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| name: RepelOptions.DM_USER, | ||||||||||||||||||||||||||||||||||||||||||||
| required: false, | ||||||||||||||||||||||||||||||||||||||||||||
| type: ApplicationCommandOptionType.Boolean, | ||||||||||||||||||||||||||||||||||||||||||||
| description: 'Whether to DM the user about the repel action (Defaults to true)', | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| execute: async (interaction) => { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -442,6 +498,17 @@ export const repelCommand = createCommand({ | |||||||||||||||||||||||||||||||||||||||||||
| lookBack: lookBack ?? DEFAULT_LOOK_BACK_MS, | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const shouldDMUser = interaction.options.getBoolean(RepelOptions.DM_USER) ?? true; | ||||||||||||||||||||||||||||||||||||||||||||
| let dmSent = false; | ||||||||||||||||||||||||||||||||||||||||||||
| if (shouldDMUser) { | ||||||||||||||||||||||||||||||||||||||||||||
| dmSent = await sendReasonToTarget({ | ||||||||||||||||||||||||||||||||||||||||||||
| target, | ||||||||||||||||||||||||||||||||||||||||||||
| reason, | ||||||||||||||||||||||||||||||||||||||||||||
| guildName: interaction.guild.name, | ||||||||||||||||||||||||||||||||||||||||||||
| timeoutDuration: timeout, | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+503
to
+510
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and then (2/2) we do this here
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| logRepelAction({ | ||||||||||||||||||||||||||||||||||||||||||||
| interaction, | ||||||||||||||||||||||||||||||||||||||||||||
| member: commandUser, | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -450,6 +517,10 @@ export const repelCommand = createCommand({ | |||||||||||||||||||||||||||||||||||||||||||
| duration: timeout, | ||||||||||||||||||||||||||||||||||||||||||||
| deleteCount: deleted, | ||||||||||||||||||||||||||||||||||||||||||||
| failedChannels, | ||||||||||||||||||||||||||||||||||||||||||||
| dm: { | ||||||||||||||||||||||||||||||||||||||||||||
| sent: dmSent, | ||||||||||||||||||||||||||||||||||||||||||||
| shouldSend: shouldDMUser, | ||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| await interaction.editReply({ | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally i would not return anything, and instead throw an error on catch, as well as console.log the error
and then ... (see next comment) 1/2