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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 428 - (show annotations)
Mon Jul 29 17:30:11 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 7005 byte(s)
refactor: use new queue handler
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, 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
28 import PunishmentType from '../../types/PunishmentType';
29 import UnmuteQueue from '../../queues/UnmuteQueue';
30
31 export async function unmute(client: DiscordClient, user: GuildMember, d: User) {
32 try {
33 const role = await user.guild!.roles.fetch(client.config.props[user.guild.id].mute_role);
34 try {
35 await user.roles.remove(role!, 'Unmuting user');
36 console.log("did that");
37 }
38 catch (e) {
39 console.log(e);
40 }
41
42 const { default: Punishment } = await import('../../models/Punishment');
43
44 const { getTimeouts, clearTimeoutv2 } = await import('../../utils/setTimeout');
45
46 const { default: Hardmute } = await import("../../models/Hardmute");
47 const { default: MuteRecord } = await import("../../models/MuteRecord");
48
49 const hardmute = await Hardmute.findOne({
50 user_id: user.id,
51 guild_id: user.guild.id
52 });
53
54 if (hardmute) {
55 for await (const roleID of hardmute.roles) {
56 try {
57 const role = await user.guild.roles.fetch(roleID);
58
59 if (role) {
60 await user.roles.add(role, 'Adding the roles which were removed due to hardmute');
61 }
62 }
63 catch (e) {
64 console.log(e);
65 }
66 }
67
68 await hardmute.delete();
69 }
70
71 // const timeouts = getTimeouts();
72
73 // for (const timeout of timeouts.values()) {
74 // if (timeout.row.params) {
75 // try {
76 // const json = JSON.parse(timeout.row.params);
77
78 // if (json) {
79 // if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
80 // await clearTimeoutv2(timeout);
81 // }
82 // }
83 // }
84 // catch (e) {
85 // console.log(e);
86 // }
87 // }
88 // }
89
90 for await (const queue of client.queueManager.queues.values()) {
91 if (queue instanceof UnmuteQueue && queue.data!.memberID === user.id && queue.data!.guildID === user.guild!.id) {
92 await queue.cancel();
93 }
94 }
95
96 await Punishment.create({
97 type: PunishmentType.UNMUTE,
98 user_id: user.id,
99 guild_id: user.guild!.id,
100 mod_id: d.id,
101 mod_tag: d.tag,
102 createdAt: new Date()
103 });
104
105 const muteRecord = await MuteRecord.findOne({
106 user_id: user.user.id,
107 guild_id: user.guild.id
108 });
109
110 if (muteRecord) {
111 await muteRecord.delete();
112 }
113
114 try {
115 await user.send({
116 embeds: [
117 new MessageEmbed()
118 .setAuthor({
119 iconURL: <string> user.guild!.iconURL(),
120 name: `\tYou have been unmuted in ${user.guild!.name}`
121 })
122 ]
123 });
124 }
125 catch (e) {
126 console.log(e);
127 }
128
129 await client.logger.logUnmute(user, d);
130 }
131 catch (e) {
132 console.log(e);
133 }
134 }
135
136 export default class UnmuteCommand extends BaseCommand {
137 supportsInteractions: boolean = true;
138 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
139
140 constructor() {
141 super('unmute', 'moderation', []);
142 }
143
144 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
145 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
146 await msg.reply({
147 embeds: [
148 new MessageEmbed()
149 .setColor('#f14a60')
150 .setDescription(`This command requires at least one argument.`)
151 ]
152 });
153
154 return;
155 }
156
157 if (msg instanceof CommandInteraction)
158 await msg.deferReply();
159
160 let user: GuildMember;
161
162 if (options.isInteraction) {
163 user = await <GuildMember> options.options.getMember('member');
164
165 if (!user) {
166 await this.deferReply(msg, {
167 embeds: [
168 new MessageEmbed()
169 .setColor('#f14a60')
170 .setDescription("Invalid user given.")
171 ]
172 });
173
174 return;
175 }
176 }
177 else {
178 try {
179 const user2 = await getMember((msg as Message), options);
180
181 if (!user2) {
182 throw new Error('Invalid user');
183 }
184
185 user = user2;
186 }
187 catch (e) {
188 await this.deferReply(msg, {
189 embeds: [
190 new MessageEmbed()
191 .setColor('#f14a60')
192 .setDescription(`Invalid user given.`)
193 ]
194 });
195
196 return;
197 }
198
199 console.log(user);
200 }
201
202 await unmute(client, user, msg.member!.user as User);
203
204 await this.deferReply(msg, {
205 embeds: [
206 new MessageEmbed()
207 .setAuthor({
208 name: user.user.tag,
209 iconURL: user.user.displayAvatarURL(),
210 })
211 .setDescription(user.user.tag + " has been unmuted.")
212 .addFields([
213 {
214 name: "Unmuted by",
215 value: (msg.member!.user as User).tag
216 },
217 ])
218 ]
219 });
220 }
221 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26