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 |
|
10 |
export default class BanCommand extends BaseCommand { |
11 |
supportsInteractions: boolean = true; |
12 |
|
13 |
constructor() { |
14 |
super('ban', 'moderation', []); |
15 |
} |
16 |
|
17 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
18 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
19 |
await msg.reply({ |
20 |
embeds: [ |
21 |
new MessageEmbed() |
22 |
.setColor('#f14a60') |
23 |
.setDescription(`This command requires at least one argument.`) |
24 |
] |
25 |
}); |
26 |
|
27 |
return; |
28 |
} |
29 |
|
30 |
let user: User; |
31 |
let banOptions: BanOptions = {}; |
32 |
|
33 |
if (options.isInteraction) { |
34 |
user = await <User> options.options.getUser('user'); |
35 |
|
36 |
if (options.options.getString('reason')) { |
37 |
banOptions.reason = await <string> options.options.getString('reason'); |
38 |
} |
39 |
|
40 |
if (options.options.getInteger('days')) { |
41 |
banOptions.days = await <number> options.options.getInteger('days'); |
42 |
} |
43 |
} |
44 |
else { |
45 |
const user2 = await getUser(client, (msg as Message), options); |
46 |
|
47 |
if (!user2) { |
48 |
await msg.reply({ |
49 |
embeds: [ |
50 |
new MessageEmbed() |
51 |
.setColor('#f14a60') |
52 |
.setDescription(`Invalid user given.`) |
53 |
] |
54 |
}); |
55 |
|
56 |
return; |
57 |
} |
58 |
|
59 |
user = user2; |
60 |
|
61 |
const index = await options.args.indexOf('-d'); |
62 |
|
63 |
if (options.args[1]) { |
64 |
const args = [...options.args]; |
65 |
args.shift(); |
66 |
|
67 |
if (index !== -1) { |
68 |
args.splice(index - 1, 2) |
69 |
} |
70 |
|
71 |
banOptions.reason = await args.join(' '); |
72 |
} |
73 |
|
74 |
if (index !== -1) { |
75 |
const days = await options.args[index + 1]; |
76 |
|
77 |
if (days === undefined) { |
78 |
await msg.reply({ |
79 |
embeds: [ |
80 |
new MessageEmbed() |
81 |
.setColor('#f14a60') |
82 |
.setDescription(`Option \`-d\` (days) requires an argument.`) |
83 |
] |
84 |
}); |
85 |
|
86 |
return; |
87 |
} |
88 |
|
89 |
if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) { |
90 |
await msg.reply({ |
91 |
embeds: [ |
92 |
new MessageEmbed() |
93 |
.setColor('#f14a60') |
94 |
.setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`) |
95 |
] |
96 |
}); |
97 |
|
98 |
return; |
99 |
} |
100 |
|
101 |
banOptions.days = await parseInt(days); |
102 |
} |
103 |
} |
104 |
|
105 |
try { |
106 |
await msg.guild?.bans.create(user, banOptions); |
107 |
await History.create(user.id, msg.guild!, 'ban', msg.member!.user.id, typeof banOptions.reason === 'undefined' ? null : banOptions.reason, async (data: any) => undefined); |
108 |
} |
109 |
catch (e) { |
110 |
await msg.reply({ |
111 |
embeds: [ |
112 |
new MessageEmbed() |
113 |
.setColor('#f14a60') |
114 |
.setDescription("Failed to ban this user. Maybe missing permisions or I'm not allowed to ban this user?") |
115 |
] |
116 |
}); |
117 |
|
118 |
return; |
119 |
} |
120 |
|
121 |
await msg.reply({ |
122 |
embeds: [ |
123 |
new MessageEmbed() |
124 |
.setAuthor({ |
125 |
name: user.tag, |
126 |
iconURL: user.displayAvatarURL(), |
127 |
}) |
128 |
.setDescription(user.tag + " has been banned from this server.") |
129 |
.addFields([ |
130 |
{ |
131 |
name: "Banned by", |
132 |
value: (msg.member!.user as User).tag |
133 |
}, |
134 |
{ |
135 |
name: "Reason", |
136 |
value: banOptions.reason === undefined ? "*No reason provided*" : banOptions.reason |
137 |
}, |
138 |
{ |
139 |
name: "Days of message deletion", |
140 |
value: banOptions.days === undefined ? "*No message will be deleted*" : (banOptions.days + '') |
141 |
} |
142 |
]) |
143 |
] |
144 |
}); |
145 |
} |
146 |
} |