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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26