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

Annotation of /trunk/src/commands/moderation/MassBanCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 194 - (hide annotations)
Mon Jul 29 17:28:58 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5904 byte(s)
feat: add proper permission checking and error messages
1 rakin 61 import { BanOptions, CommandInteraction, 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 { fetchEmoji } from '../../utils/Emoji';
10 rakin 86 import Punishment from '../../models/Punishment';
11     import PunishmentType from '../../types/PunishmentType';
12 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
13 rakin 61
14     export default class MassBanCommand extends BaseCommand {
15     supportsInteractions: boolean = true;
16    
17     constructor() {
18     super('massban', 'moderation', []);
19     }
20    
21     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
22     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
23     await msg.reply({
24     embeds: [
25     new MessageEmbed()
26     .setColor('#f14a60')
27     .setDescription(`This command requires at least one argument.`)
28     ]
29     });
30    
31     return;
32     }
33    
34     if (msg instanceof CommandInteraction)
35     await msg.deferReply();
36    
37     let arr: (string | User)[] = [];
38     let reasonStart = false;
39     let banOptions: BanOptions = {};
40    
41     if (options.isInteraction) {
42     arr = (<string> options.options.getString('users')).split(' ');
43    
44     if (options.options.getString('reason'))
45     banOptions.reason = <string> options.options.getString('reason');
46    
47     if (options.options.getInteger('days'))
48     banOptions.days = <number> options.options.getInteger('days');
49     }
50     else {
51     let i = 0;
52    
53     for await (const arg of options.args) {
54     if (!/^\d+$/g.test(arg) && !(arg.startsWith('<@') && arg.endsWith('>'))) {
55     reasonStart = true;
56     }
57    
58     if (reasonStart) {
59     banOptions.reason = banOptions.reason ?? '';
60     banOptions.reason += arg + ' ';
61     }
62     else {
63     if (/^\d+$/g.test(arg)) {
64     arr.push(arg);
65     }
66     else if ((msg as Message).mentions.users.at(i)) {
67     arr.push((msg as Message).mentions.users.at(i)! as User);
68     console.log((msg as Message).mentions.users.at(i)!);
69    
70     i++;
71     }
72     }
73     }
74    
75     if (banOptions.reason) {
76     banOptions.reason = banOptions.reason.trim();
77     }
78     }
79    
80     if (arr.length < 1) {
81     await this.deferReply(msg, {
82     embeds: [
83     new MessageEmbed()
84     .setColor('#f14a60')
85     .setDescription(`Invalid user(s) given.`)
86     ]
87     });
88    
89     return;
90     }
91    
92     let usersStr = '';
93    
94     for await (const uid of arr) {
95     try {
96     console.log(uid);
97    
98     const user = typeof uid === 'string' ? await client.users.fetch(uid) : uid;
99    
100     try {
101     console.log(banOptions.reason);
102 rakin 86
103 rakin 153 try {
104     const member = await msg.guild?.members.fetch(user.id);
105    
106 rakin 194 if (member && !(await hasPermission(client, member, msg, null, "You don't have permission to ban " + user.tag + ".")))
107     break;
108    
109 rakin 153 if (member && shouldNotModerate(client, member)) {
110     await msg.reply({
111     embeds: [
112     new MessageEmbed()
113     .setColor('#f14a60')
114     .setDescription('The user ' + user.tag + ' is not bannable.')
115     ]
116     });
117    
118     return;
119     }
120     }
121     catch (e) {
122     console.log(e);
123     }
124    
125 rakin 86 await Punishment.create({
126     type: PunishmentType.BAN,
127     user_id: user.id,
128     guild_id: msg.guild!.id,
129     mod_id: msg.member!.user.id,
130     mod_tag: (msg.member!.user as User).tag,
131     reason: banOptions.reason ?? undefined
132     });
133    
134 rakin 61 await msg.guild!.bans.create(user, banOptions);
135    
136     usersStr += user.tag + ' (' + user.id + ')\n';
137     }
138     catch (e) {
139     console.log(e);
140    
141     await this.deferReply(msg, {
142     content: 'Failed to ban ' + user.tag + ' (' + user.id + ').'
143     });
144    
145     return;
146     }
147     }
148     catch (e) {
149     console.log(e);
150    
151     await this.deferReply(msg, {
152     content: 'Invalid ID(s) given. (' + uid.toString() + ')'
153     });
154    
155     return;
156     }
157     }
158    
159     await this.deferReply(msg, {
160     embeds: [
161     new MessageEmbed()
162     .setAuthor({
163     name: 'Mass Ban'
164     })
165     .setDescription(`${(await fetchEmoji('check'))?.toString()} Mass banned the following users:\n\n${usersStr}`)
166     .addField('Reason', banOptions.reason ?? '*No reason provided*')
167     ]
168     });
169     }
170 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26