/[sudobot]/trunk/src/commands/moderation/WarningCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/moderation/WarningCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26