mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(bots/discord): add default durations for moderation commands
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user