1 |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js'; |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, Permissions, User } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
import CommandOptions from '../../types/CommandOptions'; |
10 |
import ms from 'ms'; |
import ms from 'ms'; |
11 |
import { unmute } from './UnmuteCommand'; |
import { unmute } from './UnmuteCommand'; |
12 |
import PunishmentType from '../../types/PunishmentType'; |
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) { |
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 { |
try { |
17 |
const { default: Punishment } = await import('../../models/Punishment'); |
const { default: Punishment } = await import('../../models/Punishment'); |
18 |
|
|
19 |
if (dateTime) { |
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) => { |
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) |
if (err) |
43 |
console.log(err); |
console.log(err); |
44 |
|
|
45 |
console.log('A timeout has been set.'); |
console.log('A timeout has been set.'); |
46 |
|
|
47 |
setTimeout(async () => { |
await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id); |
48 |
await client.db.get("SELECT * FROM unmutes WHERE time = ?", [new Date(dateTime!).toISOString()], async (err: any, data: any) => { |
}); |
49 |
if (err) |
} |
50 |
console.log(err); |
|
51 |
|
if (hard) { |
52 |
if (data) { |
const { default: Hardmute } = await import("../../models/Hardmute"); |
53 |
await client.db.get('DELETE FROM unmutes WHERE id = ?', [data.id], async (err: any) => { |
const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id); |
54 |
let guild = await client.guilds.cache.find(g => g.id === data.guild_id); |
await user.roles.remove(roles, reason); |
55 |
let member = await guild?.members.cache.find(m => m.id === data.user_id); |
|
56 |
|
await Hardmute.create({ |
57 |
if (member) { |
user_id: user.id, |
58 |
await unmute(client, member, msg, client.user!); |
roles: roles.map(role => role.id), |
59 |
await History.create(member.id, msg.guild!, 'unmute', client.user!.id, null); |
guild_id: msg.guild!.id, |
60 |
} |
createdAt: new Date() |
|
|
|
|
console.log(data); |
|
|
}); |
|
|
} |
|
|
}); |
|
|
}, timeInterval); |
|
61 |
}); |
}); |
62 |
} |
} |
63 |
|
|
64 |
const role = await msg.guild!.roles.fetch(client.config.get('mute_role')); |
const role = await msg.guild!.roles.fetch(client.config.get('mute_role')); |
65 |
await user.roles.add(role!); |
await user.roles.add(role!, reason); |
66 |
|
|
67 |
await Punishment.create({ |
await Punishment.create({ |
68 |
type: PunishmentType.MUTE, |
type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE, |
69 |
user_id: user.id, |
user_id: user.id, |
70 |
guild_id: msg.guild!.id, |
guild_id: msg.guild!.id, |
71 |
mod_id: msg.member!.user.id, |
mod_id: msg.member!.user.id, |
73 |
reason, |
reason, |
74 |
meta: { |
meta: { |
75 |
time: timeInterval ? ms(timeInterval) : undefined |
time: timeInterval ? ms(timeInterval) : undefined |
76 |
} |
}, |
77 |
|
createdAt: new Date() |
78 |
}); |
}); |
79 |
|
|
80 |
|
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard); |
81 |
|
|
82 |
await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason); |
try { |
83 |
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User); |
await user.send({ |
84 |
await user.send({ |
embeds: [ |
85 |
embeds: [ |
new MessageEmbed() |
86 |
new MessageEmbed() |
.setAuthor({ |
87 |
.setAuthor({ |
iconURL: <string> msg.guild!.iconURL(), |
88 |
iconURL: <string> msg.guild!.iconURL(), |
name: `\tYou have been muted in ${msg.guild!.name}` |
89 |
name: `\tYou have been muted in ${msg.guild!.name}` |
}) |
90 |
}) |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
91 |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
] |
92 |
] |
}); |
93 |
}); |
} |
94 |
|
catch (e) { |
95 |
|
console.log(e); |
96 |
|
} |
97 |
} |
} |
98 |
catch (e) { |
catch (e) { |
99 |
console.log(e); |
console.log(e); |
100 |
|
|
101 |
await msg.reply({ |
if (msg instanceof Message) |
102 |
embeds: [ |
await msg.reply({ |
103 |
new MessageEmbed() |
embeds: [ |
104 |
.setColor('#f14a60') |
new MessageEmbed() |
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?") |
.setColor('#f14a60') |
106 |
] |
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
107 |
}); |
] |
108 |
|
}); |
109 |
|
else |
110 |
|
await msg.editReply({ |
111 |
|
embeds: [ |
112 |
|
new MessageEmbed() |
113 |
|
.setColor('#f14a60') |
114 |
|
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
115 |
|
] |
116 |
|
}); |
117 |
|
|
118 |
return; |
return; |
119 |
} |
} |
121 |
|
|
122 |
export default class MuteCommand extends BaseCommand { |
export default class MuteCommand extends BaseCommand { |
123 |
supportsInteractions: boolean = true; |
supportsInteractions: boolean = true; |
124 |
|
permissions = [Permissions.FLAGS.MODERATE_MEMBERS]; |
125 |
|
|
126 |
constructor() { |
constructor() { |
127 |
super('mute', 'moderation', []); |
super('mute', 'moderation', []); |
140 |
return; |
return; |
141 |
} |
} |
142 |
|
|
143 |
|
if (msg instanceof CommandInteraction) |
144 |
|
await msg.deferReply(); |
145 |
|
|
146 |
let user: GuildMember; |
let user: GuildMember; |
147 |
let reason: string | undefined; |
let reason: string | undefined; |
148 |
let time: string | undefined; |
let time: string | undefined; |
149 |
let timeInterval: number | undefined; |
let timeInterval: number | undefined; |
150 |
let dateTime: number | undefined; |
let dateTime: number | undefined; |
151 |
|
let hard: boolean = false; |
152 |
|
|
153 |
if (options.isInteraction) { |
if (options.isInteraction) { |
154 |
user = await <GuildMember> options.options.getMember('member'); |
user = await <GuildMember> options.options.getMember('member'); |
157 |
reason = await <string> options.options.getString('reason'); |
reason = await <string> options.options.getString('reason'); |
158 |
} |
} |
159 |
|
|
160 |
|
if (options.options.getBoolean('hardmute')) { |
161 |
|
hard = await <boolean> options.options.getBoolean('hardmute'); |
162 |
|
} |
163 |
|
|
164 |
if (options.options.getString('time')) { |
if (options.options.getString('time')) { |
165 |
time = await options.options.getString('time') as string; |
time = await options.options.getString('time') as string; |
166 |
timeInterval = await ms(time); |
timeInterval = await ms(time); |
167 |
|
|
168 |
if (!timeInterval) { |
if (!timeInterval) { |
169 |
await msg.reply({ |
await this.deferReply(msg, { |
170 |
embeds: [ |
embeds: [ |
171 |
new MessageEmbed() |
new MessageEmbed() |
172 |
.setColor('#f14a60') |
.setColor('#f14a60') |
182 |
const user2 = await getMember((msg as Message), options); |
const user2 = await getMember((msg as Message), options); |
183 |
|
|
184 |
if (!user2) { |
if (!user2) { |
185 |
await msg.reply({ |
await this.deferReply(msg, { |
186 |
embeds: [ |
embeds: [ |
187 |
new MessageEmbed() |
new MessageEmbed() |
188 |
.setColor('#f14a60') |
.setColor('#f14a60') |
202 |
args.shift(); |
args.shift(); |
203 |
|
|
204 |
if (index !== -1) { |
if (index !== -1) { |
205 |
args.splice(index - 1, 2) |
args.splice(index - 1, 2); |
206 |
} |
} |
207 |
|
|
208 |
reason = await args.join(' '); |
reason = await args.join(' '); |
212 |
time = await options.args[index + 1]; |
time = await options.args[index + 1]; |
213 |
|
|
214 |
if (time === undefined) { |
if (time === undefined) { |
215 |
await msg.reply({ |
await this.deferReply(msg, { |
216 |
embeds: [ |
embeds: [ |
217 |
new MessageEmbed() |
new MessageEmbed() |
218 |
.setColor('#f14a60') |
.setColor('#f14a60') |
224 |
} |
} |
225 |
|
|
226 |
if (!ms(time)) { |
if (!ms(time)) { |
227 |
await msg.reply({ |
await this.deferReply(msg, { |
228 |
embeds: [ |
embeds: [ |
229 |
new MessageEmbed() |
new MessageEmbed() |
230 |
.setColor('#f14a60') |
.setColor('#f14a60') |
243 |
dateTime = Date.now() + timeInterval; |
dateTime = Date.now() + timeInterval; |
244 |
} |
} |
245 |
|
|
246 |
await mute(client, dateTime, user, msg, timeInterval, reason); |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) { |
247 |
|
return; |
248 |
|
} |
249 |
|
|
250 |
|
if (shouldNotModerate(client, user)) { |
251 |
|
await msg.reply({ |
252 |
|
embeds: [ |
253 |
|
{ |
254 |
|
description: "This user cannot be muted." |
255 |
|
} |
256 |
|
] |
257 |
|
}); |
258 |
|
|
259 |
|
return; |
260 |
|
} |
261 |
|
|
262 |
|
await mute(client, dateTime, user, msg, timeInterval, reason, hard); |
263 |
|
|
264 |
const fields = [ |
const fields = [ |
265 |
{ |
{ |
273 |
{ |
{ |
274 |
name: "Duration", |
name: "Duration", |
275 |
value: time === undefined ? "*No duration set*" : (time + '') |
value: time === undefined ? "*No duration set*" : (time + '') |
276 |
|
}, |
277 |
|
{ |
278 |
|
name: "Role Takeout", |
279 |
|
value: hard ? 'Yes' : 'No' |
280 |
} |
} |
281 |
]; |
]; |
282 |
|
|
283 |
console.log(fields); |
console.log(fields); |
284 |
|
|
285 |
await msg.reply({ |
await this.deferReply(msg, { |
286 |
embeds: [ |
embeds: [ |
287 |
new MessageEmbed() |
new MessageEmbed() |
288 |
.setAuthor({ |
.setAuthor({ |
294 |
] |
] |
295 |
}); |
}); |
296 |
} |
} |
|
} |
|
297 |
|
} |