From 27d3b392092141a1e3b4b0298131ff7817458dc1 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Thu, 17 Oct 2024 21:37:24 +0700 Subject: [PATCH] feat(bots/discord): add default durations for moderation commands --- bots/discord/src/commands/moderation/ban.ts | 9 +++++---- bots/discord/src/commands/moderation/mute.ts | 10 +++++----- .../src/commands/moderation/role-preset.ts | 15 +++++++++++---- bots/discord/src/commands/moderation/slowmode.ts | 9 +++------ bots/discord/src/utils/duration.ts | 11 ++++++++++- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/bots/discord/src/commands/moderation/ban.ts b/bots/discord/src/commands/moderation/ban.ts index 895ea9d..d584cd7 100644 --- a/bots/discord/src/commands/moderation/ban.ts +++ b/bots/discord/src/commands/moderation/ban.ts @@ -18,13 +18,14 @@ export default new ModerationCommand({ required: false, type: ModerationCommand.OptionType.String, }, - dmd: { - description: 'Duration to delete messages (must be from 0 to 7 days)', + dmt: { + description: + 'Time duration to delete messages (default time unit is days, must be from 0s to 7d, default value is 0s)', required: false, type: ModerationCommand.OptionType.String, }, }, - async execute({ logger, executor }, interaction, { user, reason, dmd }) { + async execute({ logger, executor }, interaction, { user, reason, dmt }) { const guild = await interaction.client.guilds.fetch(interaction.guildId) const member = await guild.members.fetch(user).catch(() => {}) const moderator = await guild.members.fetch(executor.user) @@ -40,7 +41,7 @@ export default new ModerationCommand({ ) } - const dms = Math.floor(dmd ? parseDuration(dmd) : 0 / 1000) + const dms = Math.floor((dmt ? parseDuration(dmt, 'd') : 0) / 1000) await interaction.guild!.members.ban(user, { reason: `Banned by moderator ${executor.user.tag} (${executor.id}): ${reason}`, deleteMessageSeconds: dms, diff --git a/bots/discord/src/commands/moderation/mute.ts b/bots/discord/src/commands/moderation/mute.ts index c7549a6..1e34236 100644 --- a/bots/discord/src/commands/moderation/mute.ts +++ b/bots/discord/src/commands/moderation/mute.ts @@ -14,13 +14,13 @@ export default new ModerationCommand({ required: true, type: ModerationCommand.OptionType.User, }, - reason: { - description: 'The reason for muting the member', + duration: { + description: 'The duration of the mute (default time unit is minutes)', required: false, type: ModerationCommand.OptionType.String, }, - duration: { - description: 'The duration of the mute', + reason: { + description: 'The reason for muting the member', required: false, type: ModerationCommand.OptionType.String, }, @@ -33,7 +33,7 @@ export default new ModerationCommand({ const guild = await interaction.client.guilds.fetch(interaction.guildId) const member = await guild.members.fetch(user.id) const moderator = await guild.members.fetch(executor.id) - const duration = durationInput ? parseDuration(durationInput) : Infinity + const duration = durationInput ? parseDuration(durationInput, 'm') : Infinity if (Number.isInteger(duration) && duration! < 1) throw new CommandError( diff --git a/bots/discord/src/commands/moderation/role-preset.ts b/bots/discord/src/commands/moderation/role-preset.ts index 9313fef..9e9f689 100644 --- a/bots/discord/src/commands/moderation/role-preset.ts +++ b/bots/discord/src/commands/moderation/role-preset.ts @@ -11,12 +11,12 @@ const SubcommandOptions = { type: ModerationCommand.OptionType.User, }, preset: { - description: 'The preset to apply or remove', + description: 'The preset to manage', required: true, type: ModerationCommand.OptionType.String, }, duration: { - description: 'The duration to apply the preset for (only for apply action)', + description: 'The duration to apply the preset for (only for apply action, default time unit is minutes)', required: false, type: ModerationCommand.OptionType.String, }, @@ -53,7 +53,7 @@ export default new ModerationCommand({ throw new CommandError(CommandErrorType.Generic, 'This user cannot be managed by the bot.') if (apply) { - const duration = durationInput ? parseDuration(durationInput) : Infinity + const duration = durationInput ? parseDuration(durationInput, 'm') : Infinity if (Number.isInteger(duration) && duration! < 1) throw new CommandError( CommandErrorType.InvalidArgument, @@ -83,6 +83,13 @@ export default new ModerationCommand({ removeRolePreset(member, preset) }, expires) - await sendPresetReplyAndLogs(apply ? 'apply' : 'remove', trigger, executor, user, preset, expires ? Math.ceil(expires / 1000) : undefined) + await sendPresetReplyAndLogs( + apply ? 'apply' : 'remove', + trigger, + executor, + user, + preset, + expires ? Math.ceil(expires / 1000) : undefined, + ) }, }) diff --git a/bots/discord/src/commands/moderation/slowmode.ts b/bots/discord/src/commands/moderation/slowmode.ts index 67facf7..b6a43a9 100644 --- a/bots/discord/src/commands/moderation/slowmode.ts +++ b/bots/discord/src/commands/moderation/slowmode.ts @@ -10,7 +10,7 @@ export default new ModerationCommand({ description: 'Set a slowmode for a channel', options: { duration: { - description: 'The duration to set', + description: 'The duration to set (default time unit is seconds)', required: true, type: ModerationCommand.OptionType.String, }, @@ -23,13 +23,10 @@ export default new ModerationCommand({ }, async execute({ logger, executor }, interaction, { duration: durationInput, channel: channelInput }) { const channel = channelInput ?? (await interaction.guild!.channels.fetch(interaction.channelId)) - const duration = parseDuration(durationInput) + const duration = parseDuration(durationInput, 's') if (!channel?.isTextBased() || channel.isDMBased()) - throw new CommandError( - CommandErrorType.InvalidArgument, - 'The supplied channel is not a text channel.', - ) + throw new CommandError(CommandErrorType.InvalidArgument, 'The supplied channel is not a text channel.') if (Number.isNaN(duration)) throw new CommandError(CommandErrorType.InvalidArgument, 'Invalid duration.') if (duration < 0 || duration > 36e4) diff --git a/bots/discord/src/utils/duration.ts b/bots/discord/src/utils/duration.ts index 4f4e129..ede2d76 100644 --- a/bots/discord/src/utils/duration.ts +++ b/bots/discord/src/utils/duration.ts @@ -1,6 +1,15 @@ import parse from 'parse-duration' -export const parseDuration = (duration: string) => parse(duration, 'ms') ?? Number.NaN +const defaultUnitValue = parse['']! + +export const parseDuration = (duration: string, defaultUnit?: parse.Units) => { + if (defaultUnit) parse[''] = parse[defaultUnit]! + return ( + // biome-ignore lint/suspicious/noAssignInExpressions: Expression is ignored + // biome-ignore lint/style/noCommaOperator: The last expression (parse call) is returned, it is not confusing + (parse[''] = defaultUnitValue), parse(duration, 'ms') ?? Number.NaN + ) +} export const durationToString = (duration: number) => { if (duration === 0) return '0s'