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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 428 - (hide annotations)
Mon Jul 29 17:30:11 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 7005 byte(s)
refactor: use new queue handler
1 rakin 393 /**
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 rakin 344 import { CommandInteraction, GuildMember, Message, Permissions, User } from 'discord.js';
21 rakin 51 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 rakin 86 import PunishmentType from '../../types/PunishmentType';
29 rakin 428 import UnmuteQueue from '../../queues/UnmuteQueue';
30 rakin 86
31 rakin 102 export async function unmute(client: DiscordClient, user: GuildMember, d: User) {
32 rakin 51 try {
33 rakin 106 const role = await user.guild!.roles.fetch(client.config.props[user.guild.id].mute_role);
34 rakin 235 try {
35     await user.roles.remove(role!, 'Unmuting user');
36 rakin 400 console.log("did that");
37 rakin 235 }
38     catch (e) {
39     console.log(e);
40     }
41 rakin 51
42 rakin 86 const { default: Punishment } = await import('../../models/Punishment');
43    
44 rakin 102 const { getTimeouts, clearTimeoutv2 } = await import('../../utils/setTimeout');
45    
46 rakin 124 const { default: Hardmute } = await import("../../models/Hardmute");
47 rakin 227 const { default: MuteRecord } = await import("../../models/MuteRecord");
48 rakin 124
49     const hardmute = await Hardmute.findOne({
50 rakin 333 user_id: user.id,
51     guild_id: user.guild.id
52 rakin 124 });
53    
54     if (hardmute) {
55 rakin 333 for await (const roleID of hardmute.roles) {
56 rakin 124 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 rakin 333 await hardmute.delete();
69 rakin 124 }
70    
71 rakin 428 // const timeouts = getTimeouts();
72 rakin 102
73 rakin 428 // for (const timeout of timeouts.values()) {
74     // if (timeout.row.params) {
75     // try {
76     // const json = JSON.parse(timeout.row.params);
77 rakin 102
78 rakin 428 // 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 rakin 102 }
94     }
95    
96 rakin 86 await Punishment.create({
97     type: PunishmentType.UNMUTE,
98     user_id: user.id,
99 rakin 102 guild_id: user.guild!.id,
100 rakin 86 mod_id: d.id,
101     mod_tag: d.tag,
102 rakin 336 createdAt: new Date()
103 rakin 86 });
104    
105 rakin 227 const muteRecord = await MuteRecord.findOne({
106 rakin 335 user_id: user.user.id,
107     guild_id: user.guild.id
108 rakin 227 });
109    
110     if (muteRecord) {
111 rakin 335 await muteRecord.delete();
112 rakin 227 }
113    
114 rakin 159 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 rakin 51
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 rakin 203 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
139 rakin 51
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 rakin 124 if (msg instanceof CommandInteraction)
158     await msg.deferReply();
159    
160 rakin 51 let user: GuildMember;
161    
162     if (options.isInteraction) {
163     user = await <GuildMember> options.options.getMember('member');
164    
165     if (!user) {
166 rakin 124 await this.deferReply(msg, {
167 rakin 51 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 rakin 124 await this.deferReply(msg, {
189 rakin 51 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 rakin 102 await unmute(client, user, msg.member!.user as User);
203 rakin 51
204 rakin 124 await this.deferReply(msg, {
205 rakin 51 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 rakin 159 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26