/[sudobot]/branches/5.x/src/commands/moderation/InfractionCommand.ts
ViewVC logotype

Annotation of /branches/5.x/src/commands/moderation/InfractionCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 8335 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { InfractionType } from "@prisma/client";
21     import { MessageMentions, PermissionsBitField, SlashCommandBuilder } from "discord.js";
22     import Command, { ArgumentType, BasicCommandContext, CommandMessage, CommandReturn, ValidationRule } from "../../core/Command";
23     import { isSnowflake } from "../../utils/utils";
24    
25     const infractionTypes = [
26     {
27     name: "Ban",
28     value: InfractionType.BAN
29     },
30     {
31     name: "Kick",
32     value: InfractionType.KICK
33     },
34     {
35     name: "Mute",
36     value: InfractionType.MUTE
37     },
38     {
39     name: "Warning",
40     value: InfractionType.WARNING
41     },
42     {
43     name: "Unmute",
44     value: InfractionType.UNMUTE
45     },
46     {
47     name: "Unban",
48     value: InfractionType.UNBAN
49     },
50     {
51     name: "Bulk message delete",
52     value: InfractionType.BULK_DELETE_MESSAGE
53     },
54     {
55     name: "Temporary Ban",
56     value: InfractionType.TEMPBAN
57     },
58     {
59     name: "Softban",
60     value: InfractionType.SOFTBAN
61     },
62     {
63     name: "Timeout",
64     value: InfractionType.TIMEOUT
65     },
66     {
67     name: "Timeout remove",
68     value: InfractionType.TIMEOUT_REMOVE
69     }
70     ].map(o => ({ ...o, value: o.value.toLowerCase() }));
71    
72     export default class InfractionCommand extends Command {
73     public readonly name = "infraction";
74     public readonly subcommands = ["view", "create", "edit", "delete", "list", "clear"];
75     public readonly validationRules: ValidationRule[] = [
76     {
77     types: [ArgumentType.String],
78     name: "subcommand",
79     requiredErrorMessage: `Please provide a valid subcommand! The available subcommands are: \`${this.subcommands.join("`, `")}\`.`
80     }
81     ];
82     public readonly permissions = [PermissionsBitField.Flags.ModerateMembers, PermissionsBitField.Flags.ViewAuditLog];
83     public readonly permissionMode = "or";
84     public readonly description = "Manage infractions.";
85     public readonly detailedDescription = "Use this command to manage infractions.";
86     public readonly argumentSyntaxes = ["<subcommand> [...args]"];
87    
88     public readonly slashCommandBuilder = new SlashCommandBuilder()
89     .addSubcommand(subcommand =>
90     subcommand
91     .setName("view")
92     .setDescription("View information about an infraction")
93     .addIntegerOption(option => option.setName("id").setDescription("The infraction ID").setRequired(true))
94     )
95     .addSubcommand(subcommand =>
96     subcommand
97     .setName("edit")
98     .setDescription("Update reason/duration of an infraction")
99     .addIntegerOption(option => option.setName("id").setDescription("The infraction ID").setRequired(true))
100     .addStringOption(option => option.setName("new_reason").setDescription("New reason to set"))
101     .addStringOption(option => option.setName("new_duration").setDescription("New duration to set"))
102     .addBooleanOption(option =>
103     option.setName("silent").setDescription("Specify if the bot should not let the user know about this, defaults to true")
104     )
105     )
106     .addSubcommand(subcommand =>
107     subcommand
108     .setName("delete")
109     .setDescription("Delete an infraction")
110     .addIntegerOption(option => option.setName("id").setDescription("The infraction ID").setRequired(true))
111     )
112     .addSubcommand(subcommand =>
113     subcommand
114     .setName("clear")
115     .setDescription("Clear infractions for a user")
116     .addUserOption(option => option.setName("user").setDescription("The target user").setRequired(true))
117     .addStringOption(option =>
118     option
119     .setName("type")
120     .setDescription("Specify infraction type")
121     .setChoices(...infractionTypes)
122     )
123     )
124     .addSubcommand(subcommand =>
125     subcommand
126     .setName("list")
127     .setDescription("List infractions for a user")
128     .addUserOption(option => option.setName("user").setDescription("The target user").setRequired(true))
129     )
130     .addSubcommand(subcommand =>
131     subcommand
132     .setName("create")
133     .setDescription("Add infractions to a user")
134     .addUserOption(option => option.setName("user").setDescription("The target user").setRequired(true))
135     .addStringOption(option =>
136     option
137     .setName("type")
138     .setDescription("Specify infraction type")
139     .setChoices(...infractionTypes)
140     .setRequired(true)
141     )
142     .addStringOption(option => option.setName("reason").setDescription("The reason for giving this infraction"))
143     .addStringOption(option => option.setName("duration").setDescription("The duration of this infraction (e.g. 45, 1h30m)"))
144     );
145    
146     async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
147     const subcommand = context.isLegacy ? context.parsedNamedArgs.subcommand : context.options.getSubcommand(true);
148    
149     if ((subcommand === "edit" || subcommand === "create") && context.isLegacy) {
150     await message.reply(`${this.emoji("error")} Please use the slash command \`/infraction ${subcommand}\` to perform this action.`);
151     return;
152     }
153    
154     if (["clear", "list"].includes(subcommand)) {
155     if (context.isLegacy && !context.args[1]) {
156     await message.reply(`${this.emoji("error")} Please provide a user to perform this action!`);
157     return;
158     }
159    
160     if (context.isLegacy) {
161     let userId = "";
162    
163     if (isSnowflake(context.args[1])) {
164     userId = context.args[1];
165     } else if (MessageMentions.UsersPattern.test(context.args[1])) {
166     userId = context.args[1].substring(context.args[1].includes("!") ? 3 : 2, context.args[1].length - 1);
167     } else {
168     await message.reply(`${this.emoji("error")} Please provide a valid user mention or ID to perform this action!`);
169     return;
170     }
171    
172     const user = await this.client.fetchUserSafe(userId);
173    
174     if (!user) {
175     await message.reply(`${this.emoji("error")} That user does not exist!`);
176     return;
177     }
178    
179     context.parsedNamedArgs.user = user;
180     }
181     } else {
182     if (context.isLegacy && !context.args[1]) {
183     await message.reply(`${this.emoji("error")} Please provide an infraction ID to perform this action!`);
184     return;
185     }
186    
187     if (context.isLegacy && isNaN(parseInt(context.args[1]))) {
188     await message.reply(`${this.emoji("error")} Please provide a __valid__ infraction ID to perform this action!`);
189     return;
190     }
191    
192     if (context.isLegacy) context.parsedNamedArgs.id = parseInt(context.args[1]);
193     }
194    
195     await this.deferIfInteraction(message);
196    
197     const command = this.client.commands.get(`infraction__${subcommand}`);
198    
199     if (command) {
200     return await command.execute(message, {
201     ...context
202     });
203     }
204     }
205     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26