/[sudobot]/trunk/src/commands/moderation/MuteCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/moderation/MuteCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 336 - (show annotations)
Mon Jul 29 17:29:36 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 10110 byte(s)
refactor(moderation): use mongodb
1 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, Permissions, 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 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, hard: boolean = false) {
16 try {
17 const { default: Punishment } = await import('../../models/Punishment');
18
19 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) => {
42 if (err)
43 console.log(err);
44
45 console.log('A timeout has been set.');
46
47 await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);
48 });
49 }
50
51 if (hard) {
52 const { default: Hardmute } = await import("../../models/Hardmute");
53 const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id);
54 await user.roles.remove(roles, reason);
55
56 await Hardmute.create({
57 user_id: user.id,
58 roles: roles.map(role => role.id),
59 guild_id: msg.guild!.id,
60 createdAt: new Date()
61 });
62 }
63
64 const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
65 await user.roles.add(role!, reason);
66
67 await Punishment.create({
68 type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
69 user_id: user.id,
70 guild_id: msg.guild!.id,
71 mod_id: msg.member!.user.id,
72 mod_tag: (msg.member!.user as User).tag,
73 reason,
74 meta: {
75 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 try {
83 await user.send({
84 embeds: [
85 new MessageEmbed()
86 .setAuthor({
87 iconURL: <string> msg.guild!.iconURL(),
88 name: `\tYou have been muted in ${msg.guild!.name}`
89 })
90 .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
91 ]
92 });
93 }
94 catch (e) {
95 console.log(e);
96 }
97 }
98 catch (e) {
99 console.log(e);
100
101 if (msg instanceof Message)
102 await msg.reply({
103 embeds: [
104 new MessageEmbed()
105 .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;
119 }
120 }
121
122 export default class MuteCommand extends BaseCommand {
123 supportsInteractions: boolean = true;
124 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
125
126 constructor() {
127 super('mute', 'moderation', []);
128 }
129
130 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
131 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
132 await msg.reply({
133 embeds: [
134 new MessageEmbed()
135 .setColor('#f14a60')
136 .setDescription(`This command requires at least one argument.`)
137 ]
138 });
139
140 return;
141 }
142
143 if (msg instanceof CommandInteraction)
144 await msg.deferReply();
145
146 let user: GuildMember;
147 let reason: string | undefined;
148 let time: string | undefined;
149 let timeInterval: number | undefined;
150 let dateTime: number | undefined;
151 let hard: boolean = false;
152
153 if (options.isInteraction) {
154 user = await <GuildMember> options.options.getMember('member');
155
156 if (options.options.getString('reason')) {
157 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')) {
165 time = await options.options.getString('time') as string;
166 timeInterval = await ms(time);
167
168 if (!timeInterval) {
169 await this.deferReply(msg, {
170 embeds: [
171 new MessageEmbed()
172 .setColor('#f14a60')
173 .setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`)
174 ]
175 });
176
177 return;
178 }
179 }
180 }
181 else {
182 const user2 = await getMember((msg as Message), options);
183
184 if (!user2) {
185 await this.deferReply(msg, {
186 embeds: [
187 new MessageEmbed()
188 .setColor('#f14a60')
189 .setDescription(`Invalid user given.`)
190 ]
191 });
192
193 return;
194 }
195
196 user = user2;
197
198 const index = await options.args.indexOf('-t');
199
200 if (options.args[1]) {
201 const args = [...options.args];
202 args.shift();
203
204 if (index !== -1) {
205 args.splice(index - 1, 2);
206 }
207
208 reason = await args.join(' ');
209 }
210
211 if (index !== -1) {
212 time = await options.args[index + 1];
213
214 if (time === undefined) {
215 await this.deferReply(msg, {
216 embeds: [
217 new MessageEmbed()
218 .setColor('#f14a60')
219 .setDescription(`Option \`-t\` (time) requires an argument.`)
220 ]
221 });
222
223 return;
224 }
225
226 if (!ms(time)) {
227 await this.deferReply(msg, {
228 embeds: [
229 new MessageEmbed()
230 .setColor('#f14a60')
231 .setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`)
232 ]
233 });
234
235 return;
236 }
237
238 timeInterval = await ms(time);
239 }
240 }
241
242 if (timeInterval) {
243 dateTime = Date.now() + timeInterval;
244 }
245
246 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 = [
265 {
266 name: "Muted by",
267 value: (msg.member!.user as User).tag
268 },
269 {
270 name: "Reason",
271 value: reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason
272 },
273 {
274 name: "Duration",
275 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);
284
285 await this.deferReply(msg, {
286 embeds: [
287 new MessageEmbed()
288 .setAuthor({
289 name: user.user.tag,
290 iconURL: user.displayAvatarURL(),
291 })
292 .setDescription(user.user.tag + " has been muted.")
293 .addFields(fields)
294 ]
295 });
296 }
297 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26