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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26