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