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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26