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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 336 - (show annotations)
Mon Jul 29 17:29:36 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 9664 byte(s)
refactor(moderation): use mongodb
1 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 import Punishment from '../../models/Punishment';
12 import { fetchEmoji } from '../../utils/Emoji';
13 import PunishmentType from '../../types/PunishmentType';
14
15 export default class WarningCommand extends BaseCommand {
16 supportsInteractions: boolean = true;
17
18 constructor() {
19 super('warning', 'moderation', []);
20 }
21
22 async list(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
23 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
24 await msg.reply({
25 embeds: [
26 new MessageEmbed()
27 .setColor('#f14a60')
28 .setDescription(`This command requires an argument.`)
29 ]
30 });
31
32 return;
33 }
34
35 let user: User;
36
37 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 }
43 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 }
54
55 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
61 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
70 return;
71 }
72
73 let str = '';
74
75 for await (const warning of warnings) {
76 str += `ID: ${warning.id}\n`;
77 str += `Reason: ${warning.reason ?? '*No reason provided*'}\n`;
78
79 try {
80 str += `Warned by: ${(await client.users.fetch(warning.mod_id)).tag}\n`;
81 }
82 catch (e) {
83 str += `Warned by: ${warning.mod_id}\n`;
84 }
85
86 str += `Date: ${warning.createdAt}\n\n`;
87 }
88
89 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
101 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
111 return;
112 }
113
114 let user: User;
115
116 try {
117 user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
118
119 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 const warning = await Punishment.deleteOne({
135 guild_id: msg.guild!.id,
136 user_id: user.id,
137 type: PunishmentType.WARNING
138 });
139
140 if (warning.deletedCount < 1) {
141 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 const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
175
176 const warning = await Punishment.findOne({
177 id,
178 guild_id: msg.guild!.id,
179 type: PunishmentType.WARNING
180 });
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 await warning.delete();
195
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 const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
219
220 const warning = await Punishment.findOne({
221 id,
222 guild_id: msg.guild!.id,
223 type: PunishmentType.WARNING
224 });
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
246 }
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 })
284 .setTimestamp(warning.get('createdAt') as Date)
285 ]
286 });
287 }
288
289 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
299 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 await msg.reply({
306 embeds: [
307 new MessageEmbed()
308 .setColor('#f14a60')
309 .setDescription(`Invalid subcommand given.`)
310 ]
311 });
312
313 return;
314 }
315
316 if (!options.isInteraction)
317 options.args.shift();
318
319 await (this as any)[subcmd](client, msg, options);
320 }
321 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26