/[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 341 - (hide annotations)
Mon Jul 29 17:29:38 2024 UTC (8 months, 3 weeks ago) by rakin
File MIME type: application/typescript
File size: 5932 byte(s)
refactor: removing using the old history manager
1 rakin 205 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User, Permissions } from 'discord.js';
2 rakin 61 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 { fetchEmoji } from '../../utils/Emoji';
8 rakin 86 import Punishment from '../../models/Punishment';
9     import PunishmentType from '../../types/PunishmentType';
10 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
11 rakin 61
12     export default class MassBanCommand extends BaseCommand {
13     supportsInteractions: boolean = true;
14 rakin 205
15     permissions = [Permissions.FLAGS.BAN_MEMBERS];
16 rakin 61
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 rakin 336 reason: banOptions.reason ?? undefined,
132     createdAt: new Date()
133 rakin 86 });
134    
135 rakin 61 await msg.guild!.bans.create(user, banOptions);
136    
137     usersStr += user.tag + ' (' + user.id + ')\n';
138     }
139     catch (e) {
140     console.log(e);
141    
142     await this.deferReply(msg, {
143     content: 'Failed to ban ' + user.tag + ' (' + user.id + ').'
144     });
145    
146     return;
147     }
148     }
149     catch (e) {
150     console.log(e);
151    
152     await this.deferReply(msg, {
153     content: 'Invalid ID(s) given. (' + uid.toString() + ')'
154     });
155    
156     return;
157     }
158     }
159    
160     await this.deferReply(msg, {
161     embeds: [
162     new MessageEmbed()
163     .setAuthor({
164     name: 'Mass Ban'
165     })
166     .setDescription(`${(await fetchEmoji('check'))?.toString()} Mass banned the following users:\n\n${usersStr}`)
167     .addField('Reason', banOptions.reason ?? '*No reason provided*')
168     ]
169     });
170     }
171 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26