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