1 |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, Permissions, 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 getMember from '../../utils/getMember'; |
10 |
import ms from 'ms'; |
11 |
import { unmute } from './UnmuteCommand'; |
12 |
import PunishmentType from '../../types/PunishmentType'; |
13 |
import { hasPermission, shouldNotModerate } from '../../utils/util'; |
14 |
|
15 |
export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) { |
16 |
try { |
17 |
const { default: Punishment } = await import('../../models/Punishment'); |
18 |
|
19 |
const { getTimeouts, clearTimeoutv2, setTimeoutv2 } = await import('../../utils/setTimeout'); |
20 |
|
21 |
const timeouts = getTimeouts(); |
22 |
|
23 |
for (const timeout of timeouts.values()) { |
24 |
if (timeout.row.params) { |
25 |
try { |
26 |
const json = JSON.parse(timeout.row.params); |
27 |
|
28 |
if (json) { |
29 |
if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) { |
30 |
await clearTimeoutv2(timeout); |
31 |
} |
32 |
} |
33 |
} |
34 |
catch (e) { |
35 |
console.log(e); |
36 |
} |
37 |
} |
38 |
} |
39 |
|
40 |
if (dateTime && timeInterval) { |
41 |
await client.db.get("INSERT INTO unmutes(user_id, guild_id, time) VALUES(?, ?, ?)", [user.id, msg.guild!.id, new Date(dateTime).toISOString()], async (err: any) => { |
42 |
if (err) |
43 |
console.log(err); |
44 |
|
45 |
console.log('A timeout has been set.'); |
46 |
|
47 |
await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id); |
48 |
}); |
49 |
} |
50 |
|
51 |
if (hard) { |
52 |
const { default: Hardmute } = await import("../../models/Hardmute"); |
53 |
const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id); |
54 |
await user.roles.remove(roles, reason); |
55 |
|
56 |
await Hardmute.create({ |
57 |
user_id: user.id, |
58 |
roles: roles.map(role => role.id), |
59 |
guild_id: msg.guild!.id, |
60 |
}); |
61 |
} |
62 |
|
63 |
const role = await msg.guild!.roles.fetch(client.config.get('mute_role')); |
64 |
await user.roles.add(role!, reason); |
65 |
|
66 |
await Punishment.create({ |
67 |
type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE, |
68 |
user_id: user.id, |
69 |
guild_id: msg.guild!.id, |
70 |
mod_id: msg.member!.user.id, |
71 |
mod_tag: (msg.member!.user as User).tag, |
72 |
reason, |
73 |
meta: { |
74 |
time: timeInterval ? ms(timeInterval) : undefined |
75 |
} |
76 |
}); |
77 |
|
78 |
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard); |
79 |
|
80 |
try { |
81 |
await user.send({ |
82 |
embeds: [ |
83 |
new MessageEmbed() |
84 |
.setAuthor({ |
85 |
iconURL: <string> msg.guild!.iconURL(), |
86 |
name: `\tYou have been muted in ${msg.guild!.name}` |
87 |
}) |
88 |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
89 |
] |
90 |
}); |
91 |
} |
92 |
catch (e) { |
93 |
console.log(e); |
94 |
} |
95 |
} |
96 |
catch (e) { |
97 |
console.log(e); |
98 |
|
99 |
if (msg instanceof Message) |
100 |
await msg.reply({ |
101 |
embeds: [ |
102 |
new MessageEmbed() |
103 |
.setColor('#f14a60') |
104 |
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
105 |
] |
106 |
}); |
107 |
else |
108 |
await msg.editReply({ |
109 |
embeds: [ |
110 |
new MessageEmbed() |
111 |
.setColor('#f14a60') |
112 |
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
113 |
] |
114 |
}); |
115 |
|
116 |
return; |
117 |
} |
118 |
} |
119 |
|
120 |
export default class MuteCommand extends BaseCommand { |
121 |
supportsInteractions: boolean = true; |
122 |
permissions = [Permissions.FLAGS.MODERATE_MEMBERS]; |
123 |
|
124 |
constructor() { |
125 |
super('mute', 'moderation', []); |
126 |
} |
127 |
|
128 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
129 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
130 |
await msg.reply({ |
131 |
embeds: [ |
132 |
new MessageEmbed() |
133 |
.setColor('#f14a60') |
134 |
.setDescription(`This command requires at least one argument.`) |
135 |
] |
136 |
}); |
137 |
|
138 |
return; |
139 |
} |
140 |
|
141 |
if (msg instanceof CommandInteraction) |
142 |
await msg.deferReply(); |
143 |
|
144 |
let user: GuildMember; |
145 |
let reason: string | undefined; |
146 |
let time: string | undefined; |
147 |
let timeInterval: number | undefined; |
148 |
let dateTime: number | undefined; |
149 |
let hard: boolean = false; |
150 |
|
151 |
if (options.isInteraction) { |
152 |
user = await <GuildMember> options.options.getMember('member'); |
153 |
|
154 |
if (options.options.getString('reason')) { |
155 |
reason = await <string> options.options.getString('reason'); |
156 |
} |
157 |
|
158 |
if (options.options.getBoolean('hardmute')) { |
159 |
hard = await <boolean> options.options.getBoolean('hardmute'); |
160 |
} |
161 |
|
162 |
if (options.options.getString('time')) { |
163 |
time = await options.options.getString('time') as string; |
164 |
timeInterval = await ms(time); |
165 |
|
166 |
if (!timeInterval) { |
167 |
await this.deferReply(msg, { |
168 |
embeds: [ |
169 |
new MessageEmbed() |
170 |
.setColor('#f14a60') |
171 |
.setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`) |
172 |
] |
173 |
}); |
174 |
|
175 |
return; |
176 |
} |
177 |
} |
178 |
} |
179 |
else { |
180 |
const user2 = await getMember((msg as Message), options); |
181 |
|
182 |
if (!user2) { |
183 |
await this.deferReply(msg, { |
184 |
embeds: [ |
185 |
new MessageEmbed() |
186 |
.setColor('#f14a60') |
187 |
.setDescription(`Invalid user given.`) |
188 |
] |
189 |
}); |
190 |
|
191 |
return; |
192 |
} |
193 |
|
194 |
user = user2; |
195 |
|
196 |
const index = await options.args.indexOf('-t'); |
197 |
|
198 |
if (options.args[1]) { |
199 |
const args = [...options.args]; |
200 |
args.shift(); |
201 |
|
202 |
if (index !== -1) { |
203 |
args.splice(index - 1, 2); |
204 |
} |
205 |
|
206 |
reason = await args.join(' '); |
207 |
} |
208 |
|
209 |
if (index !== -1) { |
210 |
time = await options.args[index + 1]; |
211 |
|
212 |
if (time === undefined) { |
213 |
await this.deferReply(msg, { |
214 |
embeds: [ |
215 |
new MessageEmbed() |
216 |
.setColor('#f14a60') |
217 |
.setDescription(`Option \`-t\` (time) requires an argument.`) |
218 |
] |
219 |
}); |
220 |
|
221 |
return; |
222 |
} |
223 |
|
224 |
if (!ms(time)) { |
225 |
await this.deferReply(msg, { |
226 |
embeds: [ |
227 |
new MessageEmbed() |
228 |
.setColor('#f14a60') |
229 |
.setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`) |
230 |
] |
231 |
}); |
232 |
|
233 |
return; |
234 |
} |
235 |
|
236 |
timeInterval = await ms(time); |
237 |
} |
238 |
} |
239 |
|
240 |
if (timeInterval) { |
241 |
dateTime = Date.now() + timeInterval; |
242 |
} |
243 |
|
244 |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) { |
245 |
return; |
246 |
} |
247 |
|
248 |
if (shouldNotModerate(client, user)) { |
249 |
await msg.reply({ |
250 |
embeds: [ |
251 |
{ |
252 |
description: "This user cannot be muted." |
253 |
} |
254 |
] |
255 |
}); |
256 |
|
257 |
return; |
258 |
} |
259 |
|
260 |
await mute(client, dateTime, user, msg, timeInterval, reason, hard); |
261 |
|
262 |
const fields = [ |
263 |
{ |
264 |
name: "Muted by", |
265 |
value: (msg.member!.user as User).tag |
266 |
}, |
267 |
{ |
268 |
name: "Reason", |
269 |
value: reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason |
270 |
}, |
271 |
{ |
272 |
name: "Duration", |
273 |
value: time === undefined ? "*No duration set*" : (time + '') |
274 |
}, |
275 |
{ |
276 |
name: "Role Takeout", |
277 |
value: hard ? 'Yes' : 'No' |
278 |
} |
279 |
]; |
280 |
|
281 |
console.log(fields); |
282 |
|
283 |
await this.deferReply(msg, { |
284 |
embeds: [ |
285 |
new MessageEmbed() |
286 |
.setAuthor({ |
287 |
name: user.user.tag, |
288 |
iconURL: user.displayAvatarURL(), |
289 |
}) |
290 |
.setDescription(user.user.tag + " has been muted.") |
291 |
.addFields(fields) |
292 |
] |
293 |
}); |
294 |
} |
295 |
} |