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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/commands/warning.js revision 5 by rakin, Mon Jul 29 17:28:11 2024 UTC trunk/src/commands/moderation/WarningCommand.ts revision 344 by rakin, Mon Jul 29 17:29:40 2024 UTC
# Line 1  Line 1 
1  const MessageEmbed = require("../src/MessageEmbed");  import { CommandInteraction, 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 Punishment from '../../models/Punishment';
9    import { fetchEmoji } from '../../utils/Emoji';
10    import PunishmentType from '../../types/PunishmentType';
11    
12  module.exports = {  export default class WarningCommand extends BaseCommand {
13      async handle(msg, cm) {      supportsInteractions: boolean = true;
14          if (typeof cm.args[0] === 'undefined') {  
15        constructor() {
16            super('warning', 'moderation', []);
17        }
18    
19        async list(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
20            if (!options.isInteraction && typeof options.args[0] === 'undefined') {
21              await msg.reply({              await msg.reply({
22                  embeds: [                  embeds: [
23                      new MessageEmbed()                      new MessageEmbed()
24                      .setColor('#f14a60')                      .setColor('#f14a60')
25                      .setDescription(`This command requires at least one argument.`)                      .setDescription(`This command requires an argument.`)
26                  ]                  ]
27              });              });
28    
29              return;              return;
30          }          }
31    
32          await app.db.get('SELECT * FROM warnings WHERE id = ?', [cm.args[0]], async (err, data) => {          let user: User;
             if (err) {  
                 console.log(err);  
             }  
33    
34              if (!data) {          try {
35                  await msg.reply({              user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
                     embeds: [  
                         new MessageEmbed()  
                         .setColor('#f14a60')  
                         .setDescription(`No warning found.`)  
                     ]  
                 });  
       
                 return;  
             }  
36    
37              let user = data.user_id;              if (!user)
38                    throw new Error();
39            }
40            catch (e) {
41                await msg.reply({
42                    embeds: [
43                        new MessageEmbed()
44                        .setColor('#f14a60')
45                        .setDescription(`Invalid user given.`)
46                    ]
47                });
48    
49              console.log('here1');              return;
50            }
51    
52              try {          const warnings = await Punishment.find({
53                  user = await msg.guild.members.fetch(data.user_id);              guild_id: msg.guild!.id,
54              }              user_id: user.id,
55              catch(e) {              type: PunishmentType.WARNING
56                  console.log(e);          }).sort({ createdAt: -1 });
             }  
57    
58            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    
67              console.log('user2');              return;
68            }
69    
70              let by = data.warned_by;          let str = '';
71    
72              console.log(data);          for await (const warning of warnings) {
73                str += `ID: ${warning.id}\n`;
74                str += `Reason: ${warning.reason ?? '*No reason provided*'}\n`;
75    
76              try {              try {
77                  by = await msg.guild.members.fetch(data.warned_by);                  str += `Warned by: ${(await client.users.fetch(warning.mod_id)).tag}\n`;
78              }              }
79              catch(e) {              catch (e) {
80                  console.log(e);                  str += `Warned by: ${warning.mod_id}\n`;
81              }              }
   
             console.log('here');  
             let embed = await new MessageEmbed()  
                         .setDescription(data.reason === '\c\b\c' ? "*No reason provided*" : data.reason)  
                         .addField('ID', data.id + '')  
                         .addField('Warned by', typeof by === 'string' ? by : by.user.tag);  
82                            
83              if (typeof user === 'string') {              str += `Date: ${warning.createdAt}\n\n`;
84                  embed.setAuthor({          }
85                      name: `${user}`  
86                  });          await msg.reply({
87              }              embeds: [
88              else {                  new MessageEmbed()
89                  embed.setAuthor({                  .setAuthor({
90                      iconURL: user.displayAvatarURL(),                      name: user.tag,
91                      name: `${user.user.tag}`                      iconURL: user.displayAvatarURL()
92                  })                  })
93              }                  .setDescription(`**All warnings**\n\n${str}`)
94                ]
95            });
96        }
97    
98        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    
108                return;
109            }
110    
111            let user: User;
112    
113            try {
114                user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
115    
116                if (!user)
117                    throw new Error();
118            }
119            catch (e) {
120              await msg.reply({              await msg.reply({
121                  embeds: [                  embeds: [
122                      embed                      new MessageEmbed()
123                        .setColor('#f14a60')
124                        .setDescription(`Invalid user given.`)
125                  ]                  ]
126              });              });
127    
128                return;
129            }
130    
131            const warning = await Punishment.deleteOne({
132                guild_id: msg.guild!.id,
133                user_id: user.id,
134                type: PunishmentType.WARNING
135          });          });
136    
137            if (warning.deletedCount < 1) {
138                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            const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
172    
173            const warning = await Punishment.findOne({
174                id,
175                guild_id: msg.guild!.id,
176                type: PunishmentType.WARNING
177            });
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            await warning.delete();
192    
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            const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
216    
217            const warning = await Punishment.findOne({
218                id,
219                guild_id: msg.guild!.id,
220                type: PunishmentType.WARNING
221            });
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                
243            }
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                    })
281                    .setTimestamp(warning.get('createdAt') as Date)
282                ]
283            });
284        }
285    
286        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    
296                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                await msg.reply({
303                    embeds: [
304                        new MessageEmbed()
305                        .setColor('#f14a60')
306                        .setDescription(`Invalid subcommand given.`)
307                    ]
308                });
309    
310                return;
311            }
312    
313            if (!options.isInteraction)
314                options.args.shift();
315            
316            await (this as any)[subcmd](client, msg, options);
317      }      }
 };  
318    }

Legend:
Removed from v.5  
changed lines
  Added in v.344

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26