|
| 1 | +import { type Buildable, Command } from "@framework/commands/Command"; |
| 2 | +import { TakesArgument } from "@framework/arguments/ArgumentTypes"; |
| 3 | +import type Context from "@framework/commands/Context"; |
| 4 | +import type { GuildBasedChannel } from "discord.js"; |
| 5 | +import ChannelArgument from "@framework/arguments/ChannelArgument"; |
| 6 | +import Duration from "@framework/datetime/Duration"; |
| 7 | +import DurationArgument from "@framework/arguments/DurationArgument"; |
| 8 | +import { isDiscordAPIError } from "@framework/utils/errors"; |
| 9 | + |
| 10 | +type SetSlowmodeCommandArgs = { |
| 11 | + duration: Duration; |
| 12 | + channel?: GuildBasedChannel; |
| 13 | +}; |
| 14 | + |
| 15 | +@TakesArgument<SetSlowmodeCommandArgs>({ |
| 16 | + names: ["duration"], |
| 17 | + types: [DurationArgument], |
| 18 | + optional: false, |
| 19 | + errorMessages: [DurationArgument.defaultErrors] |
| 20 | +}) |
| 21 | +@TakesArgument<SetSlowmodeCommandArgs>({ |
| 22 | + names: ["channel"], |
| 23 | + types: [ChannelArgument<true>], |
| 24 | + optional: true, |
| 25 | + errorMessages: [ChannelArgument.defaultErrors] |
| 26 | +}) |
| 27 | +class SetSlowmodeCommand extends Command { |
| 28 | + public override readonly name: string = "setslowmode"; |
| 29 | + public override readonly description: string = "Sets a slowmode for the given channel."; |
| 30 | + public override readonly defer = true; |
| 31 | + public override readonly aliases = ["slowmode", "ratelimit"]; |
| 32 | + public override readonly usage = ["<duration: Duration> [channel: Channel]"]; |
| 33 | + |
| 34 | + public override build(): Buildable[] { |
| 35 | + return [ |
| 36 | + this.buildChatInput() |
| 37 | + .addStringOption(option => |
| 38 | + option |
| 39 | + .setName("duration") |
| 40 | + .setDescription("The duration of the slowmode.") |
| 41 | + .setRequired(true) |
| 42 | + ) |
| 43 | + .addChannelOption(option => |
| 44 | + option |
| 45 | + .setName("channel") |
| 46 | + .setDescription("The channel to set the slowmode in.") |
| 47 | + .setRequired(false) |
| 48 | + ) |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public override async execute(context: Context, args: SetSlowmodeCommandArgs) { |
| 53 | + const channel = args.channel ?? context.channel; |
| 54 | + |
| 55 | + if (!channel.isTextBased()) { |
| 56 | + return void (await context.error("The channel must be a text channel.")); |
| 57 | + } |
| 58 | + |
| 59 | + if (!channel.manageable) { |
| 60 | + return void (await context.error( |
| 61 | + "The system doesn't have permission to manage this channel." |
| 62 | + )); |
| 63 | + } |
| 64 | + try { |
| 65 | + await channel.setRateLimitPerUser( |
| 66 | + args.duration.toSeconds(), |
| 67 | + `Changed by ${context.user.username} (${context.user.id})` |
| 68 | + ); |
| 69 | + } catch (error) { |
| 70 | + if (isDiscordAPIError(error)) { |
| 71 | + return void (await context.error(`Failed to set slowmode: ${error.message}`)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + await context.success( |
| 76 | + `Slowmode set to ${args.duration.toString()} in ${channel.toString()}.` |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export default SetSlowmodeCommand; |
0 commit comments