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) { |
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) { |
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 |
const role = await msg.guild!.roles.fetch(client.config.get('mute_role')); |
51 |
await user.roles.add(role!); |
52 |
|
53 |
await Punishment.create({ |
54 |
type: PunishmentType.MUTE, |
55 |
user_id: user.id, |
56 |
guild_id: msg.guild!.id, |
57 |
mod_id: msg.member!.user.id, |
58 |
mod_tag: (msg.member!.user as User).tag, |
59 |
reason, |
60 |
meta: { |
61 |
time: timeInterval ? ms(timeInterval) : undefined |
62 |
} |
63 |
}); |
64 |
|
65 |
await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason); |
66 |
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User); |
67 |
await user.send({ |
68 |
embeds: [ |
69 |
new MessageEmbed() |
70 |
.setAuthor({ |
71 |
iconURL: <string> msg.guild!.iconURL(), |
72 |
name: `\tYou have been muted in ${msg.guild!.name}` |
73 |
}) |
74 |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
75 |
] |
76 |
}); |
77 |
} |
78 |
catch (e) { |
79 |
console.log(e); |
80 |
|
81 |
await msg.reply({ |
82 |
embeds: [ |
83 |
new MessageEmbed() |
84 |
.setColor('#f14a60') |
85 |
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
86 |
] |
87 |
}); |
88 |
|
89 |
return; |
90 |
} |
91 |
} |
92 |
|
93 |
export default class MuteCommand extends BaseCommand { |
94 |
supportsInteractions: boolean = true; |
95 |
|
96 |
constructor() { |
97 |
super('mute', 'moderation', []); |
98 |
} |
99 |
|
100 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
101 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
102 |
await msg.reply({ |
103 |
embeds: [ |
104 |
new MessageEmbed() |
105 |
.setColor('#f14a60') |
106 |
.setDescription(`This command requires at least one argument.`) |
107 |
] |
108 |
}); |
109 |
|
110 |
return; |
111 |
} |
112 |
|
113 |
let user: GuildMember; |
114 |
let reason: string | undefined; |
115 |
let time: string | undefined; |
116 |
let timeInterval: number | undefined; |
117 |
let dateTime: number | undefined; |
118 |
|
119 |
if (options.isInteraction) { |
120 |
user = await <GuildMember> options.options.getMember('member'); |
121 |
|
122 |
if (options.options.getString('reason')) { |
123 |
reason = await <string> options.options.getString('reason'); |
124 |
} |
125 |
|
126 |
if (options.options.getString('time')) { |
127 |
time = await options.options.getString('time') as string; |
128 |
timeInterval = await ms(time); |
129 |
|
130 |
if (!timeInterval) { |
131 |
await msg.reply({ |
132 |
embeds: [ |
133 |
new MessageEmbed() |
134 |
.setColor('#f14a60') |
135 |
.setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`) |
136 |
] |
137 |
}); |
138 |
|
139 |
return; |
140 |
} |
141 |
} |
142 |
} |
143 |
else { |
144 |
const user2 = await getMember((msg as Message), options); |
145 |
|
146 |
if (!user2) { |
147 |
await msg.reply({ |
148 |
embeds: [ |
149 |
new MessageEmbed() |
150 |
.setColor('#f14a60') |
151 |
.setDescription(`Invalid user given.`) |
152 |
] |
153 |
}); |
154 |
|
155 |
return; |
156 |
} |
157 |
|
158 |
user = user2; |
159 |
|
160 |
const index = await options.args.indexOf('-t'); |
161 |
|
162 |
if (options.args[1]) { |
163 |
const args = [...options.args]; |
164 |
args.shift(); |
165 |
|
166 |
if (index !== -1) { |
167 |
args.splice(index - 1, 2) |
168 |
} |
169 |
|
170 |
reason = await args.join(' '); |
171 |
} |
172 |
|
173 |
if (index !== -1) { |
174 |
time = await options.args[index + 1]; |
175 |
|
176 |
if (time === undefined) { |
177 |
await msg.reply({ |
178 |
embeds: [ |
179 |
new MessageEmbed() |
180 |
.setColor('#f14a60') |
181 |
.setDescription(`Option \`-t\` (time) requires an argument.`) |
182 |
] |
183 |
}); |
184 |
|
185 |
return; |
186 |
} |
187 |
|
188 |
if (!ms(time)) { |
189 |
await msg.reply({ |
190 |
embeds: [ |
191 |
new MessageEmbed() |
192 |
.setColor('#f14a60') |
193 |
.setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`) |
194 |
] |
195 |
}); |
196 |
|
197 |
return; |
198 |
} |
199 |
|
200 |
timeInterval = await ms(time); |
201 |
} |
202 |
} |
203 |
|
204 |
if (timeInterval) { |
205 |
dateTime = Date.now() + timeInterval; |
206 |
} |
207 |
|
208 |
await mute(client, dateTime, user, msg, timeInterval, reason); |
209 |
|
210 |
const fields = [ |
211 |
{ |
212 |
name: "Muted by", |
213 |
value: (msg.member!.user as User).tag |
214 |
}, |
215 |
{ |
216 |
name: "Reason", |
217 |
value: reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason |
218 |
}, |
219 |
{ |
220 |
name: "Duration", |
221 |
value: time === undefined ? "*No duration set*" : (time + '') |
222 |
} |
223 |
]; |
224 |
|
225 |
console.log(fields); |
226 |
|
227 |
await msg.reply({ |
228 |
embeds: [ |
229 |
new MessageEmbed() |
230 |
.setAuthor({ |
231 |
name: user.user.tag, |
232 |
iconURL: user.displayAvatarURL(), |
233 |
}) |
234 |
.setDescription(user.user.tag + " has been muted.") |
235 |
.addFields(fields) |
236 |
] |
237 |
}); |
238 |
} |
239 |
} |