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