1 |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, User } from 'discord.js'; |
/** |
2 |
|
* This file is part of SudoBot. |
3 |
|
* |
4 |
|
* Copyright (C) 2021-2022 OSN Inc. |
5 |
|
* |
6 |
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU Affero General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
*/ |
19 |
|
|
20 |
|
import { CommandInteraction, Guild, GuildMember, Message, Permissions, User } from 'discord.js'; |
21 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
import BaseCommand from '../../utils/structures/BaseCommand'; |
22 |
import DiscordClient from '../../client/Client'; |
import DiscordClient from '../../client/Client'; |
23 |
import CommandOptions from '../../types/CommandOptions'; |
import CommandOptions from '../../types/CommandOptions'; |
24 |
import InteractionOptions from '../../types/InteractionOptions'; |
import InteractionOptions from '../../types/InteractionOptions'; |
25 |
import MessageEmbed from '../../client/MessageEmbed'; |
import MessageEmbed from '../../client/MessageEmbed'; |
|
import getUser from '../../utils/getUser'; |
|
|
import History from '../../automod/History'; |
|
26 |
import getMember from '../../utils/getMember'; |
import getMember from '../../utils/getMember'; |
27 |
import ms from 'ms'; |
import ms from 'ms'; |
|
import { unmute } from './UnmuteCommand'; |
|
28 |
import PunishmentType from '../../types/PunishmentType'; |
import PunishmentType from '../../types/PunishmentType'; |
29 |
|
import { hasPermission, shouldNotModerate } from '../../utils/util'; |
30 |
|
import UnmuteQueue from '../../queues/UnmuteQueue'; |
31 |
|
|
32 |
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 | { guild: Guild, member: GuildMember, editReply?: undefined }, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) { |
33 |
try { |
try { |
34 |
const { default: Punishment } = await import('../../models/Punishment'); |
const { default: Punishment } = await import('../../models/Punishment'); |
35 |
|
|
43 |
const json = JSON.parse(timeout.row.params); |
const json = JSON.parse(timeout.row.params); |
44 |
|
|
45 |
if (json) { |
if (json) { |
46 |
if (json[1] === user.id) { |
if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) { |
47 |
await clearTimeoutv2(timeout); |
await clearTimeoutv2(timeout); |
48 |
} |
} |
49 |
} |
} |
55 |
} |
} |
56 |
|
|
57 |
if (dateTime && timeInterval) { |
if (dateTime && timeInterval) { |
58 |
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 setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id); |
59 |
if (err) |
for await (const queue of client.queueManager.queues.values()) { |
60 |
console.log(err); |
if (queue instanceof UnmuteQueue && queue.data!.memberID === user.id && queue.data!.guildID === msg.guild!.id) { |
61 |
|
await queue.cancel(); |
62 |
console.log('A timeout has been set.'); |
} |
63 |
|
} |
64 |
|
|
65 |
await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id); |
await client.queueManager.addQueue(UnmuteQueue, { |
66 |
|
data: { |
67 |
|
guildID: msg.guild!.id, |
68 |
|
memberID: user.id |
69 |
|
}, |
70 |
|
runAt: new Date(Date.now() + timeInterval), |
71 |
|
guild: msg.guild!.id |
72 |
}); |
}); |
73 |
} |
} |
74 |
|
|
75 |
const role = await msg.guild!.roles.fetch(client.config.get('mute_role')); |
if (hard) { |
76 |
await user.roles.add(role!); |
const { default: Hardmute } = await import("../../models/Hardmute"); |
77 |
|
const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id); |
78 |
|
await user.roles.remove(roles, reason); |
79 |
|
|
80 |
|
await Hardmute.create({ |
81 |
|
user_id: user.id, |
82 |
|
roles: roles.map(role => role.id), |
83 |
|
guild_id: msg.guild!.id, |
84 |
|
createdAt: new Date() |
85 |
|
}); |
86 |
|
} |
87 |
|
|
88 |
|
const role = await msg.guild!.roles.fetch(client.config.props[msg.guild!.id].mute_role); |
89 |
|
await user.roles.add(role!, reason); |
90 |
|
|
91 |
await Punishment.create({ |
await Punishment.create({ |
92 |
type: PunishmentType.MUTE, |
type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE, |
93 |
user_id: user.id, |
user_id: user.id, |
94 |
guild_id: msg.guild!.id, |
guild_id: msg.guild!.id, |
95 |
mod_id: msg.member!.user.id, |
mod_id: msg.member!.user.id, |
97 |
reason, |
reason, |
98 |
meta: { |
meta: { |
99 |
time: timeInterval ? ms(timeInterval) : undefined |
time: timeInterval ? ms(timeInterval) : undefined |
100 |
} |
}, |
101 |
|
createdAt: new Date() |
102 |
}); |
}); |
103 |
|
|
104 |
|
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard); |
105 |
|
|
106 |
await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason); |
try { |
107 |
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User); |
await user.send({ |
108 |
await user.send({ |
embeds: [ |
109 |
embeds: [ |
new MessageEmbed() |
110 |
new MessageEmbed() |
.setAuthor({ |
111 |
.setAuthor({ |
iconURL: <string> msg.guild!.iconURL(), |
112 |
iconURL: <string> msg.guild!.iconURL(), |
name: `\tYou have been muted in ${msg.guild!.name}` |
113 |
name: `\tYou have been muted in ${msg.guild!.name}` |
}) |
114 |
}) |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
115 |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
] |
116 |
] |
}); |
117 |
}); |
} |
118 |
|
catch (e) { |
119 |
|
console.log(e); |
120 |
|
} |
121 |
} |
} |
122 |
catch (e) { |
catch (e) { |
123 |
console.log(e); |
console.log(e); |
124 |
|
|
125 |
await msg.reply({ |
if (msg instanceof Message) |
126 |
embeds: [ |
await msg.reply({ |
127 |
new MessageEmbed() |
embeds: [ |
128 |
.setColor('#f14a60') |
new MessageEmbed() |
129 |
.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') |
130 |
] |
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
131 |
}); |
] |
132 |
|
}); |
133 |
|
else if (msg.editReply) |
134 |
|
await msg.editReply({ |
135 |
|
embeds: [ |
136 |
|
new MessageEmbed() |
137 |
|
.setColor('#f14a60') |
138 |
|
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
139 |
|
] |
140 |
|
}); |
141 |
|
|
142 |
return; |
return; |
143 |
} |
} |
145 |
|
|
146 |
export default class MuteCommand extends BaseCommand { |
export default class MuteCommand extends BaseCommand { |
147 |
supportsInteractions: boolean = true; |
supportsInteractions: boolean = true; |
148 |
|
permissions = [Permissions.FLAGS.MODERATE_MEMBERS]; |
149 |
|
|
150 |
constructor() { |
constructor() { |
151 |
super('mute', 'moderation', []); |
super('mute', 'moderation', []); |
164 |
return; |
return; |
165 |
} |
} |
166 |
|
|
167 |
|
if (msg instanceof CommandInteraction) |
168 |
|
await msg.deferReply(); |
169 |
|
|
170 |
let user: GuildMember; |
let user: GuildMember; |
171 |
let reason: string | undefined; |
let reason: string | undefined; |
172 |
let time: string | undefined; |
let time: string | undefined; |
173 |
let timeInterval: number | undefined; |
let timeInterval: number | undefined; |
174 |
let dateTime: number | undefined; |
let dateTime: number | undefined; |
175 |
|
let hard: boolean = false; |
176 |
|
|
177 |
if (options.isInteraction) { |
if (options.isInteraction) { |
178 |
user = await <GuildMember> options.options.getMember('member'); |
user = await <GuildMember> options.options.getMember('member'); |
181 |
reason = await <string> options.options.getString('reason'); |
reason = await <string> options.options.getString('reason'); |
182 |
} |
} |
183 |
|
|
184 |
|
if (options.options.getBoolean('hardmute')) { |
185 |
|
hard = await <boolean> options.options.getBoolean('hardmute'); |
186 |
|
} |
187 |
|
|
188 |
if (options.options.getString('time')) { |
if (options.options.getString('time')) { |
189 |
time = await options.options.getString('time') as string; |
time = await options.options.getString('time') as string; |
190 |
timeInterval = await ms(time); |
timeInterval = await ms(time); |
191 |
|
|
192 |
if (!timeInterval) { |
if (!timeInterval) { |
193 |
await msg.reply({ |
await this.deferReply(msg, { |
194 |
embeds: [ |
embeds: [ |
195 |
new MessageEmbed() |
new MessageEmbed() |
196 |
.setColor('#f14a60') |
.setColor('#f14a60') |
206 |
const user2 = await getMember((msg as Message), options); |
const user2 = await getMember((msg as Message), options); |
207 |
|
|
208 |
if (!user2) { |
if (!user2) { |
209 |
await msg.reply({ |
await this.deferReply(msg, { |
210 |
embeds: [ |
embeds: [ |
211 |
new MessageEmbed() |
new MessageEmbed() |
212 |
.setColor('#f14a60') |
.setColor('#f14a60') |
226 |
args.shift(); |
args.shift(); |
227 |
|
|
228 |
if (index !== -1) { |
if (index !== -1) { |
229 |
args.splice(index - 1, 2) |
args.splice(index - 1, 2); |
230 |
} |
} |
231 |
|
|
232 |
reason = await args.join(' '); |
reason = await args.join(' '); |
236 |
time = await options.args[index + 1]; |
time = await options.args[index + 1]; |
237 |
|
|
238 |
if (time === undefined) { |
if (time === undefined) { |
239 |
await msg.reply({ |
await this.deferReply(msg, { |
240 |
embeds: [ |
embeds: [ |
241 |
new MessageEmbed() |
new MessageEmbed() |
242 |
.setColor('#f14a60') |
.setColor('#f14a60') |
248 |
} |
} |
249 |
|
|
250 |
if (!ms(time)) { |
if (!ms(time)) { |
251 |
await msg.reply({ |
await this.deferReply(msg, { |
252 |
embeds: [ |
embeds: [ |
253 |
new MessageEmbed() |
new MessageEmbed() |
254 |
.setColor('#f14a60') |
.setColor('#f14a60') |
267 |
dateTime = Date.now() + timeInterval; |
dateTime = Date.now() + timeInterval; |
268 |
} |
} |
269 |
|
|
270 |
await mute(client, dateTime, user, msg, timeInterval, reason); |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) { |
271 |
|
return; |
272 |
|
} |
273 |
|
|
274 |
|
if (shouldNotModerate(client, user)) { |
275 |
|
await msg.reply({ |
276 |
|
embeds: [ |
277 |
|
{ |
278 |
|
description: "This user cannot be muted." |
279 |
|
} |
280 |
|
] |
281 |
|
}); |
282 |
|
|
283 |
|
return; |
284 |
|
} |
285 |
|
|
286 |
|
await mute(client, dateTime, user, msg, timeInterval, reason, hard); |
287 |
|
|
288 |
const fields = [ |
const fields = [ |
289 |
{ |
{ |
297 |
{ |
{ |
298 |
name: "Duration", |
name: "Duration", |
299 |
value: time === undefined ? "*No duration set*" : (time + '') |
value: time === undefined ? "*No duration set*" : (time + '') |
300 |
|
}, |
301 |
|
{ |
302 |
|
name: "Role Takeout", |
303 |
|
value: hard ? 'Yes' : 'No' |
304 |
} |
} |
305 |
]; |
]; |
306 |
|
|
307 |
console.log(fields); |
console.log(fields); |
308 |
|
|
309 |
await msg.reply({ |
await this.deferReply(msg, { |
310 |
embeds: [ |
embeds: [ |
311 |
new MessageEmbed() |
new MessageEmbed() |
312 |
.setAuthor({ |
.setAuthor({ |
318 |
] |
] |
319 |
}); |
}); |
320 |
} |
} |
|
} |
|
321 |
|
} |