/[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 86 by rakin, Mon Jul 29 17:28:32 2024 UTC
# Line 1  Line 1 
1  const MessageEmbed = require("../src/MessageEmbed");  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  module.exports = {  export default class WarningCommand extends BaseCommand {
16      async handle(msg, cm) {      supportsInteractions: boolean = true;
17          if (typeof cm.args[0] === 'undefined') {  
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({              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          await app.db.get('SELECT * FROM warnings WHERE id = ?', [cm.args[0]], async (err, data) => {          let user: User;
             if (err) {  
                 console.log(err);  
             }  
36    
37              if (!data) {          try {
38                  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;  
             }  
39    
40              let user = data.user_id;              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              console.log('here1');              return;
53            }
54    
55              try {          const warnings = await Punishment.findAll({
56                  user = await msg.guild.members.fetch(data.user_id);              where: {
57              }                  guild_id: msg.guild!.id,
58              catch(e) {                  user_id: user.id,
59                  console.log(e);                  type: PunishmentType.WARNING
60              }              },
61            });
62    
63            if (warnings.length < 1) {
64                await msg.reply({
65                    embeds: [
66                        new MessageEmbed()
67                        .setColor('#f14a60')
68                        .setDescription(`No warnings found for that user.`)
69                    ]
70                });
71    
72              console.log('user2');              return;
73            }
74    
75              let by = data.warned_by;          let str = '';
76    
77              console.log(data);          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                  by = await msg.guild.members.fetch(data.warned_by);                  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              }              }
   
             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);  
87                            
88              if (typeof user === 'string') {              str += `Date: ${warning.get().createdAt}\n\n`;
89                  embed.setAuthor({          }
90                      name: `${user}`  
91                  });          await msg.reply({
92              }              embeds: [
93              else {                  new MessageEmbed()
94                  embed.setAuthor({                  .setAuthor({
95                      iconURL: user.displayAvatarURL(),                      name: user.tag,
96                      name: `${user.user.tag}`                      iconURL: user.displayAvatarURL()
97                  })                  })
98              }                  .setDescription(`**All warnings**\n\n${str}`)
99                ]
100            });
101        }
102    
103        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                return;
114            }
115    
116            let user: User;
117    
118            try {
119                user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
120    
121                if (!user)
122                    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                return;
176            }
177    
178            const id = options.isInteraction ? options.options.getNumber('id') : parseInt(options.args[0]);
179    
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({              await msg.reply({
193                  embeds: [                  embeds: [
194                      embed                      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            }
261    
262            let user: User | string = warning.get('user_id') as string;
263            
264            try {
265                user = await client.users.fetch(user);
266            }
267            catch (e) {
268                
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({
320                    embeds: [
321                        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.5  
changed lines
  Added in v.86

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26