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 |
continue; |
121 |
} |
122 |
|
123 |
await Punishment.create({ |
124 |
type: PunishmentType.BAN, |
125 |
user_id: user.id, |
126 |
guild_id: msg.guild!.id, |
127 |
mod_id: msg.member!.user.id, |
128 |
mod_tag: (msg.member!.user as User).tag, |
129 |
reason: banOptions.reason ?? undefined |
130 |
}); |
131 |
|
132 |
await msg.guild!.bans.create(user, banOptions); |
133 |
|
134 |
usersStr += user.tag + ' (' + user.id + ')\n'; |
135 |
} |
136 |
catch (e) { |
137 |
console.log(e); |
138 |
|
139 |
await this.deferReply(msg, { |
140 |
content: 'Failed to ban ' + user.tag + ' (' + user.id + ').' |
141 |
}); |
142 |
|
143 |
return; |
144 |
} |
145 |
} |
146 |
catch (e) { |
147 |
console.log(e); |
148 |
|
149 |
await this.deferReply(msg, { |
150 |
content: 'Invalid ID(s) given. (' + uid.toString() + ')' |
151 |
}); |
152 |
|
153 |
return; |
154 |
} |
155 |
} |
156 |
|
157 |
await this.deferReply(msg, { |
158 |
embeds: [ |
159 |
new MessageEmbed() |
160 |
.setAuthor({ |
161 |
name: 'Mass Ban' |
162 |
}) |
163 |
.setDescription(`${(await fetchEmoji('check'))?.toString()} Mass banned the following users:\n\n${usersStr}`) |
164 |
.addField('Reason', banOptions.reason ?? '*No reason provided*') |
165 |
] |
166 |
}); |
167 |
} |
168 |
} |