1 |
rakin |
106 |
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 |
|
|
import { fetchEmojiStr } from '../../utils/Emoji'; |
12 |
rakin |
153 |
import { shouldNotModerate } from '../../utils/util'; |
13 |
rakin |
106 |
|
14 |
|
|
export default class SoftBanCommand extends BaseCommand { |
15 |
|
|
supportsInteractions: boolean = true; |
16 |
|
|
|
17 |
|
|
constructor() { |
18 |
|
|
super('softban', 'moderation', []); |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
22 |
|
|
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
23 |
|
|
await msg.reply({ |
24 |
|
|
embeds: [ |
25 |
|
|
new MessageEmbed() |
26 |
|
|
.setColor('#f14a60') |
27 |
|
|
.setDescription(`This command requires at least one argument.`) |
28 |
|
|
] |
29 |
|
|
}); |
30 |
|
|
|
31 |
|
|
return; |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
let user: User; |
35 |
|
|
let banOptions: BanOptions = { |
36 |
|
|
days: 7 |
37 |
|
|
}; |
38 |
|
|
|
39 |
|
|
if (options.isInteraction) { |
40 |
|
|
user = await <User> options.options.getUser('user'); |
41 |
|
|
|
42 |
|
|
if (options.options.getString('reason')) { |
43 |
|
|
banOptions.reason = await <string> options.options.getString('reason'); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
if (options.options.getInteger('days')) { |
47 |
|
|
banOptions.days = await <number> options.options.getInteger('days'); |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
|
else { |
51 |
|
|
const user2 = await getUser(client, (msg as Message), options); |
52 |
|
|
|
53 |
|
|
if (!user2) { |
54 |
|
|
await msg.reply({ |
55 |
|
|
embeds: [ |
56 |
|
|
new MessageEmbed() |
57 |
|
|
.setColor('#f14a60') |
58 |
|
|
.setDescription(`Invalid user given.`) |
59 |
|
|
] |
60 |
|
|
}); |
61 |
|
|
|
62 |
|
|
return; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
user = user2; |
66 |
|
|
|
67 |
|
|
const index = await options.args.indexOf('-d'); |
68 |
|
|
|
69 |
|
|
if (options.args[1]) { |
70 |
|
|
const args = [...options.args]; |
71 |
|
|
args.shift(); |
72 |
|
|
|
73 |
|
|
if (index !== -1) { |
74 |
|
|
args.splice(index - 1, 2) |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
banOptions.reason = await args.join(' '); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if (index !== -1) { |
81 |
|
|
const days = await options.args[index + 1]; |
82 |
|
|
|
83 |
|
|
if (days === undefined) { |
84 |
|
|
await msg.reply({ |
85 |
|
|
embeds: [ |
86 |
|
|
new MessageEmbed() |
87 |
|
|
.setColor('#f14a60') |
88 |
|
|
.setDescription(`Option \`-d\` (days) requires an argument.`) |
89 |
|
|
] |
90 |
|
|
}); |
91 |
|
|
|
92 |
|
|
return; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
if (!parseInt(days) || parseInt(days) < 0 || parseInt(days) > 7) { |
96 |
|
|
await msg.reply({ |
97 |
|
|
embeds: [ |
98 |
|
|
new MessageEmbed() |
99 |
|
|
.setColor('#f14a60') |
100 |
|
|
.setDescription(`Option \`-d\` (days) requires an argument which must be a valid number and in range of 0-7.`) |
101 |
|
|
] |
102 |
|
|
}); |
103 |
|
|
|
104 |
|
|
return; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
banOptions.days = await parseInt(days); |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
let reply = await msg.reply({ |
112 |
|
|
embeds: [ |
113 |
|
|
new MessageEmbed({ |
114 |
|
|
author: { |
115 |
|
|
name: user.tag, |
116 |
|
|
iconURL: user.displayAvatarURL() |
117 |
|
|
}, |
118 |
|
|
description: `${await fetchEmojiStr('loading')} Softban is in progress...`, |
119 |
|
|
color: 'GOLD' |
120 |
|
|
}) |
121 |
|
|
] |
122 |
|
|
}); |
123 |
|
|
|
124 |
|
|
if (msg instanceof CommandInteraction) |
125 |
|
|
reply = <Message> await msg.fetchReply(); |
126 |
|
|
|
127 |
|
|
try { |
128 |
rakin |
153 |
try { |
129 |
|
|
const member = await msg.guild!.members.fetch(user.id); |
130 |
|
|
|
131 |
|
|
if (member && shouldNotModerate(client, member)) { |
132 |
|
|
await msg.reply({ |
133 |
|
|
embeds: [ |
134 |
|
|
new MessageEmbed() |
135 |
|
|
.setColor('#f14a60') |
136 |
|
|
.setDescription('This user cannot be softbanned.') |
137 |
|
|
] |
138 |
|
|
}); |
139 |
|
|
|
140 |
|
|
return; |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
catch (e) { |
144 |
|
|
console.log(e); |
145 |
|
|
return; |
146 |
|
|
} |
147 |
|
|
|
148 |
rakin |
106 |
await msg.guild?.bans.create(user, banOptions); |
149 |
|
|
await new Promise(r => setTimeout(r, 1600)); |
150 |
|
|
await msg.guild?.bans.remove(user); |
151 |
|
|
|
152 |
|
|
const punishment = await Punishment.create({ |
153 |
|
|
type: PunishmentType.SOFTBAN, |
154 |
|
|
user_id: user.id, |
155 |
|
|
guild_id: msg.guild!.id, |
156 |
|
|
mod_id: msg.member!.user.id, |
157 |
|
|
mod_tag: (msg.member!.user as User).tag, |
158 |
|
|
reason: banOptions.reason ?? undefined, |
159 |
|
|
meta: { |
160 |
|
|
days: banOptions.days |
161 |
|
|
} |
162 |
|
|
}); |
163 |
|
|
|
164 |
|
|
await client.logger.logSoftBan(banOptions, msg.guild!, user, punishment); |
165 |
|
|
|
166 |
|
|
await reply!.edit({ |
167 |
|
|
embeds: [ |
168 |
|
|
new MessageEmbed({ |
169 |
|
|
author: { |
170 |
|
|
name: user.tag, |
171 |
|
|
iconURL: user.displayAvatarURL() |
172 |
|
|
}, |
173 |
|
|
description: `${await fetchEmojiStr('check')} Softbanned user ${user.tag}`, |
174 |
|
|
fields: [ |
175 |
|
|
{ |
176 |
|
|
name: 'Softbanned by', |
177 |
|
|
value: (<User> msg.member?.user).tag |
178 |
|
|
}, |
179 |
|
|
{ |
180 |
|
|
name: 'Reason', |
181 |
|
|
value: banOptions.reason ?? '*No reason provided*' |
182 |
|
|
} |
183 |
|
|
] |
184 |
|
|
}) |
185 |
|
|
] |
186 |
|
|
}); |
187 |
|
|
} |
188 |
|
|
catch (e) { |
189 |
|
|
await reply!.edit({ |
190 |
|
|
embeds: [ |
191 |
|
|
new MessageEmbed() |
192 |
|
|
.setColor('#f14a60') |
193 |
|
|
.setDescription("Failed to softban this user. Maybe missing permisions or I'm not allowed to ban this user?") |
194 |
|
|
] |
195 |
|
|
}); |
196 |
|
|
|
197 |
|
|
return; |
198 |
|
|
} |
199 |
|
|
} |
200 |
rakin |
153 |
} |