/[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 336 by rakin, Mon Jul 29 17:29:36 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 '../../models/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.find({
56              if (err) {              guild_id: msg.guild!.id,
57                  console.log(err);              user_id: user.id,
58              }              type: PunishmentType.WARNING
59            }).sort({ createdAt: -1 });
60    
61              if (!data) {          if (warnings.length < 1) {
62                  await msg.reply({              await msg.reply({
63                      embeds: [                  embeds: [
64                          new MessageEmbed()                      new MessageEmbed()
65                          .setColor('#f14a60')                      .setColor('#f14a60')
66                          .setDescription(`No warning found.`)                      .setDescription(`No warnings found for that user.`)
67                      ]                  ]
68                  });              });
       
                 return;  
             }  
69    
70              let user = data.user_id;              return;
71            }
72    
73            let str = '';
74    
75              console.log('here1');          for await (const warning of warnings) {
76                str += `ID: ${warning.id}\n`;
77                str += `Reason: ${warning.reason ?? '*No reason provided*'}\n`;
78    
79              try {              try {
80                  user = await msg.guild!.members.fetch(data.user_id);                  str += `Warned by: ${(await client.users.fetch(warning.mod_id)).tag}\n`;
81              }              }
82              catch(e) {              catch (e) {
83                  console.log(e);                  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              console.log('user2');      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              let by = data.warned_by;              return;
112            }
113    
114              console.log(data);          let user: User;
115    
116              try {          try {
117                  by = await msg.guild!.members.fetch(data.warned_by);              user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!;
118              }  
119              catch(e) {              if (!user)
120                  console.log(e);                  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              console.log('here');              return;
172              let embed = await new MessageEmbed()          }
173                          .setDescription(data.reason === '\c\b\c' ? "*No reason provided*" : data.reason)  
174                          .addField('ID', data.id + '')          const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]);
175                          .addField('Warned by', typeof by === 'string' ? by : by.user.tag);  
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              if (typeof user === 'string') {          }
247                  embed.setAuthor({  
248                      name: `${user}`          let user: User | string = warning.get('user_id') as string;
249                  });          
250              }          try {
251              else {              user = await client.users.fetch(user);
252                  embed.setAuthor({          }
253                      iconURL: user.displayAvatarURL(),          catch (e) {
254                      name: `${user.user.tag}`              
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({              await msg.reply({
306                  embeds: [                  embeds: [
307                      embed                      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  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26