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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26