mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-18 00:33:59 +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,
|
required: false,
|
||||||
type: ModerationCommand.OptionType.String,
|
type: ModerationCommand.OptionType.String,
|
||||||
},
|
},
|
||||||
dmd: {
|
dmt: {
|
||||||
description: 'Duration to delete messages (must be from 0 to 7 days)',
|
description:
|
||||||
|
'Time duration to delete messages (default time unit is days, must be from 0s to 7d, default value is 0s)',
|
||||||
required: false,
|
required: false,
|
||||||
type: ModerationCommand.OptionType.String,
|
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 guild = await interaction.client.guilds.fetch(interaction.guildId)
|
||||||
const member = await guild.members.fetch(user).catch(() => {})
|
const member = await guild.members.fetch(user).catch(() => {})
|
||||||
const moderator = await guild.members.fetch(executor.user)
|
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, {
|
await interaction.guild!.members.ban(user, {
|
||||||
reason: `Banned by moderator ${executor.user.tag} (${executor.id}): ${reason}`,
|
reason: `Banned by moderator ${executor.user.tag} (${executor.id}): ${reason}`,
|
||||||
deleteMessageSeconds: dms,
|
deleteMessageSeconds: dms,
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ export default new ModerationCommand({
|
|||||||
required: true,
|
required: true,
|
||||||
type: ModerationCommand.OptionType.User,
|
type: ModerationCommand.OptionType.User,
|
||||||
},
|
},
|
||||||
reason: {
|
duration: {
|
||||||
description: 'The reason for muting the member',
|
description: 'The duration of the mute (default time unit is minutes)',
|
||||||
required: false,
|
required: false,
|
||||||
type: ModerationCommand.OptionType.String,
|
type: ModerationCommand.OptionType.String,
|
||||||
},
|
},
|
||||||
duration: {
|
reason: {
|
||||||
description: 'The duration of the mute',
|
description: 'The reason for muting the member',
|
||||||
required: false,
|
required: false,
|
||||||
type: ModerationCommand.OptionType.String,
|
type: ModerationCommand.OptionType.String,
|
||||||
},
|
},
|
||||||
@@ -33,7 +33,7 @@ export default new ModerationCommand({
|
|||||||
const guild = await interaction.client.guilds.fetch(interaction.guildId)
|
const guild = await interaction.client.guilds.fetch(interaction.guildId)
|
||||||
const member = await guild.members.fetch(user.id)
|
const member = await guild.members.fetch(user.id)
|
||||||
const moderator = await guild.members.fetch(executor.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)
|
if (Number.isInteger(duration) && duration! < 1)
|
||||||
throw new CommandError(
|
throw new CommandError(
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ const SubcommandOptions = {
|
|||||||
type: ModerationCommand.OptionType.User,
|
type: ModerationCommand.OptionType.User,
|
||||||
},
|
},
|
||||||
preset: {
|
preset: {
|
||||||
description: 'The preset to apply or remove',
|
description: 'The preset to manage',
|
||||||
required: true,
|
required: true,
|
||||||
type: ModerationCommand.OptionType.String,
|
type: ModerationCommand.OptionType.String,
|
||||||
},
|
},
|
||||||
duration: {
|
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,
|
required: false,
|
||||||
type: ModerationCommand.OptionType.String,
|
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.')
|
throw new CommandError(CommandErrorType.Generic, 'This user cannot be managed by the bot.')
|
||||||
|
|
||||||
if (apply) {
|
if (apply) {
|
||||||
const duration = durationInput ? parseDuration(durationInput) : Infinity
|
const duration = durationInput ? parseDuration(durationInput, 'm') : Infinity
|
||||||
if (Number.isInteger(duration) && duration! < 1)
|
if (Number.isInteger(duration) && duration! < 1)
|
||||||
throw new CommandError(
|
throw new CommandError(
|
||||||
CommandErrorType.InvalidArgument,
|
CommandErrorType.InvalidArgument,
|
||||||
@@ -83,6 +83,13 @@ export default new ModerationCommand({
|
|||||||
removeRolePreset(member, preset)
|
removeRolePreset(member, preset)
|
||||||
}, expires)
|
}, 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',
|
description: 'Set a slowmode for a channel',
|
||||||
options: {
|
options: {
|
||||||
duration: {
|
duration: {
|
||||||
description: 'The duration to set',
|
description: 'The duration to set (default time unit is seconds)',
|
||||||
required: true,
|
required: true,
|
||||||
type: ModerationCommand.OptionType.String,
|
type: ModerationCommand.OptionType.String,
|
||||||
},
|
},
|
||||||
@@ -23,13 +23,10 @@ export default new ModerationCommand({
|
|||||||
},
|
},
|
||||||
async execute({ logger, executor }, interaction, { duration: durationInput, channel: channelInput }) {
|
async execute({ logger, executor }, interaction, { duration: durationInput, channel: channelInput }) {
|
||||||
const channel = channelInput ?? (await interaction.guild!.channels.fetch(interaction.channelId))
|
const channel = channelInput ?? (await interaction.guild!.channels.fetch(interaction.channelId))
|
||||||
const duration = parseDuration(durationInput)
|
const duration = parseDuration(durationInput, 's')
|
||||||
|
|
||||||
if (!channel?.isTextBased() || channel.isDMBased())
|
if (!channel?.isTextBased() || channel.isDMBased())
|
||||||
throw new CommandError(
|
throw new CommandError(CommandErrorType.InvalidArgument, 'The supplied channel is not a text channel.')
|
||||||
CommandErrorType.InvalidArgument,
|
|
||||||
'The supplied channel is not a text channel.',
|
|
||||||
)
|
|
||||||
|
|
||||||
if (Number.isNaN(duration)) throw new CommandError(CommandErrorType.InvalidArgument, 'Invalid duration.')
|
if (Number.isNaN(duration)) throw new CommandError(CommandErrorType.InvalidArgument, 'Invalid duration.')
|
||||||
if (duration < 0 || duration > 36e4)
|
if (duration < 0 || duration > 36e4)
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
import parse from 'parse-duration'
|
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) => {
|
export const durationToString = (duration: number) => {
|
||||||
if (duration === 0) return '0s'
|
if (duration === 0) return '0s'
|
||||||
|
|||||||
Reference in New Issue
Block a user