/[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

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

Legend:
Removed from v.51  
changed lines
  Added in v.85

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26