/[sudobot]/branches/3.x/src/commands/moderation/MuteCommand.ts
ViewVC logotype

Contents of /branches/3.x/src/commands/moderation/MuteCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26