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