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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (show annotations)
Mon Jul 29 17:28:56 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5742 byte(s)
fix: mention reading strategy (#41)

* fix(commands): ban related commands not working when banning users outside of the server

* fix(utils): reading users/members in the wrong way
1 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 import Punishment from '../../models/Punishment';
11 import PunishmentType from '../../types/PunishmentType';
12 import { shouldNotModerate } from '../../utils/util';
13
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
103 try {
104 const member = await msg.guild?.members.fetch(user.id);
105
106 if (member && shouldNotModerate(client, member)) {
107 await msg.reply({
108 embeds: [
109 new MessageEmbed()
110 .setColor('#f14a60')
111 .setDescription('The user ' + user.tag + ' is not bannable.')
112 ]
113 });
114
115 return;
116 }
117 }
118 catch (e) {
119 console.log(e);
120 }
121
122 await Punishment.create({
123 type: PunishmentType.BAN,
124 user_id: user.id,
125 guild_id: msg.guild!.id,
126 mod_id: msg.member!.user.id,
127 mod_tag: (msg.member!.user as User).tag,
128 reason: banOptions.reason ?? undefined
129 });
130
131 await msg.guild!.bans.create(user, banOptions);
132
133 usersStr += user.tag + ' (' + user.id + ')\n';
134 }
135 catch (e) {
136 console.log(e);
137
138 await this.deferReply(msg, {
139 content: 'Failed to ban ' + user.tag + ' (' + user.id + ').'
140 });
141
142 return;
143 }
144 }
145 catch (e) {
146 console.log(e);
147
148 await this.deferReply(msg, {
149 content: 'Invalid ID(s) given. (' + uid.toString() + ')'
150 });
151
152 return;
153 }
154 }
155
156 await this.deferReply(msg, {
157 embeds: [
158 new MessageEmbed()
159 .setAuthor({
160 name: 'Mass Ban'
161 })
162 .setDescription(`${(await fetchEmoji('check'))?.toString()} Mass banned the following users:\n\n${usersStr}`)
163 .addField('Reason', banOptions.reason ?? '*No reason provided*')
164 ]
165 });
166 }
167 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26