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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26