/[sudobot]/branches/4.x/src/commands/moderation/WarningCommand.ts
ViewVC logotype

Contents of /branches/4.x/src/commands/moderation/WarningCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months, 1 week ago) by rakinar2
File MIME type: application/typescript
File size: 10298 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2022 OSN Inc.
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 { CommandInteraction, Message, Permissions, User } from 'discord.js';
21 import BaseCommand from '../../utils/structures/BaseCommand';
22 import DiscordClient from '../../client/Client';
23 import CommandOptions from '../../types/CommandOptions';
24 import InteractionOptions from '../../types/InteractionOptions';
25 import MessageEmbed from '../../client/MessageEmbed';
26 import getUser from '../../utils/getUser';
27 import Punishment from '../../models/Punishment';
28 import { fetchEmoji } from '../../utils/Emoji';
29 import PunishmentType from '../../types/PunishmentType';
30
31 export default class WarningCommand extends BaseCommand {
32 supportsInteractions: boolean = true;
33 permissions = [Permissions.FLAGS.MANAGE_MESSAGES];
34
35 constructor() {
36 super('warning', 'moderation', []);
37 }
38
39 async list(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
40 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
41 await msg.reply({
42 embeds: [
43 new MessageEmbed()
44 .setColor('#f14a60')
45 .setDescription(`This command requires an argument.`)
46 ]
47 });
48
49 return;
50 }
51
52 let user: User;
53
54 try {
55 user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
56
57 if (!user)
58 throw new Error();
59 }
60 catch (e) {
61 await msg.reply({
62 embeds: [
63 new MessageEmbed()
64 .setColor('#f14a60')
65 .setDescription(`Invalid user given.`)
66 ]
67 });
68
69 return;
70 }
71
72 const warnings = await Punishment.find({
73 guild_id: msg.guild!.id,
74 user_id: user.id,
75 type: PunishmentType.WARNING
76 }).sort({ createdAt: -1 });
77
78 if (warnings.length < 1) {
79 await msg.reply({
80 embeds: [
81 new MessageEmbed()
82 .setColor('#f14a60')
83 .setDescription(`No warnings found for that user.`)
84 ]
85 });
86
87 return;
88 }
89
90 let str = '';
91
92 for await (const warning of warnings) {
93 str += `ID: ${warning.id}\n`;
94 str += `Reason: ${warning.reason ?? '*No reason provided*'}\n`;
95
96 try {
97 str += `Warned by: ${(await client.users.fetch(warning.mod_id)).tag}\n`;
98 }
99 catch (e) {
100 str += `Warned by: ${warning.mod_id}\n`;
101 }
102
103 str += `Date: ${warning.createdAt}\n\n`;
104 }
105
106 await msg.reply({
107 embeds: [
108 new MessageEmbed()
109 .setAuthor({
110 name: user.tag,
111 iconURL: user.displayAvatarURL()
112 })
113 .setDescription(`**All warnings**\n\n${str}`)
114 ]
115 });
116 }
117
118 async clear(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
119 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
120 await msg.reply({
121 embeds: [
122 new MessageEmbed()
123 .setColor('#f14a60')
124 .setDescription(`This command requires an argument.`)
125 ]
126 });
127
128 return;
129 }
130
131 let user: User;
132
133 try {
134 user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
135
136 if (!user)
137 throw new Error();
138 }
139 catch (e) {
140 await msg.reply({
141 embeds: [
142 new MessageEmbed()
143 .setColor('#f14a60')
144 .setDescription(`Invalid user given.`)
145 ]
146 });
147
148 return;
149 }
150
151 const warning = await Punishment.deleteOne({
152 guild_id: msg.guild!.id,
153 user_id: user.id,
154 type: PunishmentType.WARNING
155 });
156
157 if (warning.deletedCount < 1) {
158 await msg.reply({
159 embeds: [
160 new MessageEmbed()
161 .setColor('#f14a60')
162 .setDescription(`No warnings found for that user.`)
163 ]
164 });
165
166 return;
167 }
168
169 await msg.reply({
170 embeds: [
171 new MessageEmbed()
172 .setColor('GREEN')
173 .setDescription(`${(await fetchEmoji('check'))?.toString()} Cleared ${warning} warnings for ${user.tag}`)
174 ]
175 });
176 }
177
178 async remove(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
179 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
180 await msg.reply({
181 embeds: [
182 new MessageEmbed()
183 .setColor('#f14a60')
184 .setDescription(`This command requires an argument.`)
185 ]
186 });
187
188 return;
189 }
190
191 const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
192
193 const warning = await Punishment.findOne({
194 id,
195 guild_id: msg.guild!.id,
196 type: PunishmentType.WARNING
197 });
198
199 if (!warning) {
200 await msg.reply({
201 embeds: [
202 new MessageEmbed()
203 .setColor('#f14a60')
204 .setDescription(`Invalid warning ID given.`)
205 ]
206 });
207
208 return;
209 }
210
211 await warning.delete();
212
213 await msg.reply({
214 embeds: [
215 new MessageEmbed()
216 .setColor('GREEN')
217 .setDescription(`${(await fetchEmoji('check'))?.toString()} Warning removed successfully!`)
218 ]
219 });
220 }
221
222 async view(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
223 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
224 await msg.reply({
225 embeds: [
226 new MessageEmbed()
227 .setColor('#f14a60')
228 .setDescription(`This command requires an argument.`)
229 ]
230 });
231
232 return;
233 }
234
235 const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
236
237 const warning = await Punishment.findOne({
238 id,
239 guild_id: msg.guild!.id,
240 type: PunishmentType.WARNING
241 });
242
243 if (!warning) {
244 await msg.reply({
245 embeds: [
246 new MessageEmbed()
247 .setColor('#f14a60')
248 .setDescription(`Invalid warning ID given.`)
249 ]
250 });
251
252 return;
253 }
254
255 let mod: string = warning.get('mod_id') as string;
256
257 try {
258 const m = await client.users.fetch(mod);
259 mod = m.tag;
260 }
261 catch (e) {
262
263 }
264
265 let user: User | string = warning.get('user_id') as string;
266
267 try {
268 user = await client.users.fetch(user);
269 }
270 catch (e) {
271
272 }
273
274 const fields = [
275 {
276 name: 'Warning ID',
277 value: (warning.get('id') as number) + ''
278 },
279 {
280 name: 'Reason',
281 value: warning.get('reason') as string|null ?? '*No reason provided*'
282 },
283 {
284 name: 'Warned by',
285 value: mod
286 },
287 ];
288
289 console.log(fields);
290
291
292 await msg.reply({
293 embeds: [
294 new MessageEmbed({
295 author: {
296 name: typeof user === 'string' ? user : user.tag,
297 iconURL: typeof user === 'string' ? undefined : user.displayAvatarURL()
298 },
299 fields,
300 })
301 .setTimestamp(warning.get('createdAt') as Date)
302 ]
303 });
304 }
305
306 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
307 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
308 await msg.reply({
309 embeds: [
310 new MessageEmbed()
311 .setColor('#f14a60')
312 .setDescription(`This command requires a subcommand.`)
313 ]
314 });
315
316 return;
317 }
318
319 const subcmd = options.isInteraction ? options.options.getSubcommand(true) : options.args[0];
320
321 if (!['view', 'remove', 'clear', 'list'].includes(subcmd)) {
322 await msg.reply({
323 embeds: [
324 new MessageEmbed()
325 .setColor('#f14a60')
326 .setDescription(`Invalid subcommand given.`)
327 ]
328 });
329
330 return;
331 }
332
333 if (!options.isInteraction)
334 options.args.shift();
335
336 await (this as any)[subcmd](client, msg, options);
337 }
338 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26