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