/[sudobot]/branches/4.x/src/commands/moderation/UnmuteCommand.ts
ViewVC logotype

Annotation of /branches/4.x/src/commands/moderation/UnmuteCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26