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