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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 341 - (hide annotations)
Mon Jul 29 17:29:38 2024 UTC (8 months, 3 weeks ago) by rakin
File MIME type: application/typescript
File size: 9980 byte(s)
refactor: removing using the old history manager
1 rakin 203 import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, Permissions, User } from 'discord.js';
2 rakin 51 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 getMember from '../../utils/getMember';
8     import ms from 'ms';
9 rakin 86 import PunishmentType from '../../types/PunishmentType';
10 rakin 194 import { hasPermission, shouldNotModerate } from '../../utils/util';
11 rakin 51
12 rakin 124 export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction, timeInterval: number | undefined, reason: string | undefined, hard: boolean = false) {
13 rakin 51 try {
14 rakin 86 const { default: Punishment } = await import('../../models/Punishment');
15    
16 rakin 102 const { getTimeouts, clearTimeoutv2, setTimeoutv2 } = await import('../../utils/setTimeout');
17    
18     const timeouts = getTimeouts();
19    
20     for (const timeout of timeouts.values()) {
21     if (timeout.row.params) {
22     try {
23     const json = JSON.parse(timeout.row.params);
24    
25     if (json) {
26 rakin 106 if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
27 rakin 102 await clearTimeoutv2(timeout);
28     }
29     }
30     }
31     catch (e) {
32     console.log(e);
33     }
34     }
35     }
36    
37     if (dateTime && timeInterval) {
38 rakin 51 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) => {
39     if (err)
40     console.log(err);
41    
42     console.log('A timeout has been set.');
43    
44 rakin 102 await setTimeoutv2('unmute-job', timeInterval, msg.guild!.id, `unmute ${user.id}`, msg.guild!.id, user.id);
45 rakin 51 });
46     }
47 rakin 102
48 rakin 124 if (hard) {
49     const { default: Hardmute } = await import("../../models/Hardmute");
50     const roles = await user.roles.cache.filter(r => r.id !== msg.guild!.id);
51     await user.roles.remove(roles, reason);
52    
53     await Hardmute.create({
54     user_id: user.id,
55     roles: roles.map(role => role.id),
56     guild_id: msg.guild!.id,
57 rakin 333 createdAt: new Date()
58 rakin 124 });
59     }
60    
61 rakin 51 const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
62 rakin 124 await user.roles.add(role!, reason);
63 rakin 86
64     await Punishment.create({
65 rakin 124 type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
66 rakin 86 user_id: user.id,
67     guild_id: msg.guild!.id,
68     mod_id: msg.member!.user.id,
69     mod_tag: (msg.member!.user as User).tag,
70     reason,
71     meta: {
72     time: timeInterval ? ms(timeInterval) : undefined
73 rakin 336 },
74     createdAt: new Date()
75 rakin 86 });
76 rakin 124
77     await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
78 rakin 159
79     try {
80     await user.send({
81     embeds: [
82     new MessageEmbed()
83     .setAuthor({
84     iconURL: <string> msg.guild!.iconURL(),
85     name: `\tYou have been muted in ${msg.guild!.name}`
86     })
87     .addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason)
88     ]
89     });
90     }
91     catch (e) {
92     console.log(e);
93     }
94 rakin 51 }
95     catch (e) {
96     console.log(e);
97    
98 rakin 124 if (msg instanceof Message)
99     await msg.reply({
100     embeds: [
101     new MessageEmbed()
102     .setColor('#f14a60')
103     .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
104     ]
105     });
106     else
107     await msg.editReply({
108     embeds: [
109     new MessageEmbed()
110     .setColor('#f14a60')
111     .setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?")
112     ]
113     });
114 rakin 51
115     return;
116     }
117     }
118    
119     export default class MuteCommand extends BaseCommand {
120     supportsInteractions: boolean = true;
121 rakin 203 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
122 rakin 51
123     constructor() {
124     super('mute', 'moderation', []);
125     }
126    
127     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
128     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
129     await msg.reply({
130     embeds: [
131     new MessageEmbed()
132     .setColor('#f14a60')
133     .setDescription(`This command requires at least one argument.`)
134     ]
135     });
136    
137     return;
138     }
139    
140 rakin 124 if (msg instanceof CommandInteraction)
141     await msg.deferReply();
142    
143 rakin 51 let user: GuildMember;
144     let reason: string | undefined;
145     let time: string | undefined;
146     let timeInterval: number | undefined;
147     let dateTime: number | undefined;
148 rakin 124 let hard: boolean = false;
149 rakin 51
150     if (options.isInteraction) {
151     user = await <GuildMember> options.options.getMember('member');
152    
153     if (options.options.getString('reason')) {
154     reason = await <string> options.options.getString('reason');
155     }
156    
157 rakin 124 if (options.options.getBoolean('hardmute')) {
158     hard = await <boolean> options.options.getBoolean('hardmute');
159     }
160    
161 rakin 51 if (options.options.getString('time')) {
162     time = await options.options.getString('time') as string;
163     timeInterval = await ms(time);
164    
165     if (!timeInterval) {
166 rakin 124 await this.deferReply(msg, {
167 rakin 51 embeds: [
168     new MessageEmbed()
169     .setColor('#f14a60')
170     .setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`)
171     ]
172     });
173    
174     return;
175     }
176     }
177     }
178     else {
179     const user2 = await getMember((msg as Message), options);
180    
181     if (!user2) {
182 rakin 124 await this.deferReply(msg, {
183 rakin 51 embeds: [
184     new MessageEmbed()
185     .setColor('#f14a60')
186     .setDescription(`Invalid user given.`)
187     ]
188     });
189    
190     return;
191     }
192    
193     user = user2;
194    
195     const index = await options.args.indexOf('-t');
196    
197     if (options.args[1]) {
198     const args = [...options.args];
199     args.shift();
200    
201     if (index !== -1) {
202 rakin 124 args.splice(index - 1, 2);
203 rakin 51 }
204    
205     reason = await args.join(' ');
206     }
207    
208     if (index !== -1) {
209     time = await options.args[index + 1];
210    
211     if (time === undefined) {
212 rakin 124 await this.deferReply(msg, {
213 rakin 51 embeds: [
214     new MessageEmbed()
215     .setColor('#f14a60')
216     .setDescription(`Option \`-t\` (time) requires an argument.`)
217     ]
218     });
219    
220     return;
221     }
222    
223     if (!ms(time)) {
224 rakin 124 await this.deferReply(msg, {
225 rakin 51 embeds: [
226     new MessageEmbed()
227     .setColor('#f14a60')
228     .setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`)
229     ]
230     });
231    
232     return;
233     }
234    
235     timeInterval = await ms(time);
236     }
237     }
238    
239     if (timeInterval) {
240     dateTime = Date.now() + timeInterval;
241     }
242    
243 rakin 194 if (!(await hasPermission(client, user, msg, null, "You don't have permission to mute this user."))) {
244     return;
245     }
246    
247 rakin 153 if (shouldNotModerate(client, user)) {
248     await msg.reply({
249     embeds: [
250     {
251     description: "This user cannot be muted."
252     }
253     ]
254     });
255    
256     return;
257     }
258    
259 rakin 124 await mute(client, dateTime, user, msg, timeInterval, reason, hard);
260 rakin 51
261     const fields = [
262     {
263     name: "Muted by",
264     value: (msg.member!.user as User).tag
265     },
266     {
267     name: "Reason",
268     value: reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason
269     },
270     {
271     name: "Duration",
272     value: time === undefined ? "*No duration set*" : (time + '')
273 rakin 124 },
274     {
275     name: "Role Takeout",
276     value: hard ? 'Yes' : 'No'
277 rakin 51 }
278     ];
279    
280     console.log(fields);
281    
282 rakin 124 await this.deferReply(msg, {
283 rakin 51 embeds: [
284     new MessageEmbed()
285     .setAuthor({
286     name: user.user.tag,
287     iconURL: user.displayAvatarURL(),
288     })
289     .setDescription(user.user.tag + " has been muted.")
290     .addFields(fields)
291     ]
292     });
293     }
294 rakin 153 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26