feat(bots/discord): add default durations for moderation commands

This commit is contained in:
PalmDevs
2024-10-17 21:37:24 +07:00
parent 6e181c0e7f
commit 27d3b39209
5 changed files with 34 additions and 20 deletions

View File

@@ -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,

View File

@@ -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(

View File

@@ -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,
)
},
})

View File

@@ -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)

View File

@@ -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'