/[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 153 - (hide annotations)
Mon Jul 29 17:28:48 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 9746 byte(s)
Add Non-moderable role (#34)

* feat(utils): add shouldNotModerate() function

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26