/[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 153 - (show 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 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 import { 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 });
61 }
62
63 const role = await msg.guild!.roles.fetch(client.config.get('mute_role'));
64 await user.roles.add(role!, reason);
65
66 await Punishment.create({
67 type: hard ? PunishmentType.HARDMUTE : PunishmentType.MUTE,
68 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
78 await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User, hard);
79 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 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
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 if (msg instanceof CommandInteraction)
135 await msg.deferReply();
136
137 let user: GuildMember;
138 let reason: string | undefined;
139 let time: string | undefined;
140 let timeInterval: number | undefined;
141 let dateTime: number | undefined;
142 let hard: boolean = false;
143
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 if (options.options.getBoolean('hardmute')) {
152 hard = await <boolean> options.options.getBoolean('hardmute');
153 }
154
155 if (options.options.getString('time')) {
156 time = await options.options.getString('time') as string;
157 timeInterval = await ms(time);
158
159 if (!timeInterval) {
160 await this.deferReply(msg, {
161 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 await this.deferReply(msg, {
177 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 args.splice(index - 1, 2);
197 }
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 await this.deferReply(msg, {
207 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 await this.deferReply(msg, {
219 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 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 await mute(client, dateTime, user, msg, timeInterval, reason, hard);
250
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 },
264 {
265 name: "Role Takeout",
266 value: hard ? 'Yes' : 'No'
267 }
268 ];
269
270 console.log(fields);
271
272 await this.deferReply(msg, {
273 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 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26