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, |
75 |
time: timeInterval ? ms(timeInterval) : undefined |
time: timeInterval ? ms(timeInterval) : undefined |
76 |
} |
} |
77 |
}); |
}); |
78 |
|
|
79 |
|
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard); |
80 |
|
|
81 |
await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason); |
try { |
82 |
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User); |
await user.send({ |
83 |
await user.send({ |
embeds: [ |
84 |
embeds: [ |
new MessageEmbed() |
85 |
new MessageEmbed() |
.setAuthor({ |
86 |
.setAuthor({ |
iconURL: <string> msg.guild!.iconURL(), |
87 |
iconURL: <string> msg.guild!.iconURL(), |
name: `\tYou have been muted in ${msg.guild!.name}` |
88 |
name: `\tYou have been muted in ${msg.guild!.name}` |
}) |
89 |
}) |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
90 |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
] |
91 |
] |
}); |
92 |
}); |
} |
93 |
|
catch (e) { |
94 |
|
console.log(e); |
95 |
|
} |
96 |
} |
} |
97 |
catch (e) { |
catch (e) { |
98 |
console.log(e); |
console.log(e); |
99 |
|
|
100 |
await msg.reply({ |
if (msg instanceof Message) |
101 |
embeds: [ |
await msg.reply({ |
102 |
new MessageEmbed() |
embeds: [ |
103 |
.setColor('#f14a60') |
new MessageEmbed() |
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?") |
.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 |
|
else |
109 |
|
await msg.editReply({ |
110 |
|
embeds: [ |
111 |
|
new MessageEmbed() |
112 |
|
.setColor('#f14a60') |
113 |
|
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
114 |
|
] |
115 |
|
}); |
116 |
|
|
117 |
return; |
return; |
118 |
} |
} |
120 |
|
|
121 |
export default class MuteCommand extends BaseCommand { |
export default class MuteCommand extends BaseCommand { |
122 |
supportsInteractions: boolean = true; |
supportsInteractions: boolean = true; |
123 |
|
permissions = [Permissions.FLAGS.MODERATE_MEMBERS]; |
124 |
|
|
125 |
constructor() { |
constructor() { |
126 |
super('mute', 'moderation', []); |
super('mute', 'moderation', []); |
139 |
return; |
return; |
140 |
} |
} |
141 |
|
|
142 |
|
if (msg instanceof CommandInteraction) |
143 |
|
await msg.deferReply(); |
144 |
|
|
145 |
let user: GuildMember; |
let user: GuildMember; |
146 |
let reason: string | undefined; |
let reason: string | undefined; |
147 |
let time: string | undefined; |
let time: string | undefined; |
148 |
let timeInterval: number | undefined; |
let timeInterval: number | undefined; |
149 |
let dateTime: number | undefined; |
let dateTime: number | undefined; |
150 |
|
let hard: boolean = false; |
151 |
|
|
152 |
if (options.isInteraction) { |
if (options.isInteraction) { |
153 |
user = await <GuildMember> options.options.getMember('member'); |
user = await <GuildMember> options.options.getMember('member'); |
156 |
reason = await <string> options.options.getString('reason'); |
reason = await <string> options.options.getString('reason'); |
157 |
} |
} |
158 |
|
|
159 |
|
if (options.options.getBoolean('hardmute')) { |
160 |
|
hard = await <boolean> options.options.getBoolean('hardmute'); |
161 |
|
} |
162 |
|
|
163 |
if (options.options.getString('time')) { |
if (options.options.getString('time')) { |
164 |
time = await options.options.getString('time') as string; |
time = await options.options.getString('time') as string; |
165 |
timeInterval = await ms(time); |
timeInterval = await ms(time); |
166 |
|
|
167 |
if (!timeInterval) { |
if (!timeInterval) { |
168 |
await msg.reply({ |
await this.deferReply(msg, { |
169 |
embeds: [ |
embeds: [ |
170 |
new MessageEmbed() |
new MessageEmbed() |
171 |
.setColor('#f14a60') |
.setColor('#f14a60') |
181 |
const user2 = await getMember((msg as Message), options); |
const user2 = await getMember((msg as Message), options); |
182 |
|
|
183 |
if (!user2) { |
if (!user2) { |
184 |
await msg.reply({ |
await this.deferReply(msg, { |
185 |
embeds: [ |
embeds: [ |
186 |
new MessageEmbed() |
new MessageEmbed() |
187 |
.setColor('#f14a60') |
.setColor('#f14a60') |
201 |
args.shift(); |
args.shift(); |
202 |
|
|
203 |
if (index !== -1) { |
if (index !== -1) { |
204 |
args.splice(index - 1, 2) |
args.splice(index - 1, 2); |
205 |
} |
} |
206 |
|
|
207 |
reason = await args.join(' '); |
reason = await args.join(' '); |
211 |
time = await options.args[index + 1]; |
time = await options.args[index + 1]; |
212 |
|
|
213 |
if (time === undefined) { |
if (time === undefined) { |
214 |
await msg.reply({ |
await this.deferReply(msg, { |
215 |
embeds: [ |
embeds: [ |
216 |
new MessageEmbed() |
new MessageEmbed() |
217 |
.setColor('#f14a60') |
.setColor('#f14a60') |
223 |
} |
} |
224 |
|
|
225 |
if (!ms(time)) { |
if (!ms(time)) { |
226 |
await msg.reply({ |
await this.deferReply(msg, { |
227 |
embeds: [ |
embeds: [ |
228 |
new MessageEmbed() |
new MessageEmbed() |
229 |
.setColor('#f14a60') |
.setColor('#f14a60') |
242 |
dateTime = Date.now() + timeInterval; |
dateTime = Date.now() + timeInterval; |
243 |
} |
} |
244 |
|
|
245 |
await mute(client, dateTime, user, msg, timeInterval, reason); |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) { |
246 |
|
return; |
247 |
|
} |
248 |
|
|
249 |
|
if (shouldNotModerate(client, user)) { |
250 |
|
await msg.reply({ |
251 |
|
embeds: [ |
252 |
|
{ |
253 |
|
description: "This user cannot be muted." |
254 |
|
} |
255 |
|
] |
256 |
|
}); |
257 |
|
|
258 |
|
return; |
259 |
|
} |
260 |
|
|
261 |
|
await mute(client, dateTime, user, msg, timeInterval, reason, hard); |
262 |
|
|
263 |
const fields = [ |
const fields = [ |
264 |
{ |
{ |
272 |
{ |
{ |
273 |
name: "Duration", |
name: "Duration", |
274 |
value: time === undefined ? "*No duration set*" : (time + '') |
value: time === undefined ? "*No duration set*" : (time + '') |
275 |
|
}, |
276 |
|
{ |
277 |
|
name: "Role Takeout", |
278 |
|
value: hard ? 'Yes' : 'No' |
279 |
} |
} |
280 |
]; |
]; |
281 |
|
|
282 |
console.log(fields); |
console.log(fields); |
283 |
|
|
284 |
await msg.reply({ |
await this.deferReply(msg, { |
285 |
embeds: [ |
embeds: [ |
286 |
new MessageEmbed() |
new MessageEmbed() |
287 |
.setAuthor({ |
.setAuthor({ |
293 |
] |
] |
294 |
}); |
}); |
295 |
} |
} |
|
} |
|
296 |
|
} |