/[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 336 - (hide annotations)
Mon Jul 29 17:29:36 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 6098 byte(s)
refactor(moderation): 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 rakin 336 createdAt: new Date()
81 rakin 86 });
82    
83 rakin 227 const muteRecord = await MuteRecord.findOne({
84 rakin 335 user_id: user.user.id,
85     guild_id: user.guild.id
86 rakin 227 });
87    
88     if (muteRecord) {
89 rakin 335 await muteRecord.delete();
90 rakin 227 }
91    
92 rakin 159 try {
93     await user.send({
94     embeds: [
95     new MessageEmbed()
96     .setAuthor({
97     iconURL: <string> user.guild!.iconURL(),
98     name: `\tYou have been unmuted in ${user.guild!.name}`
99     })
100     ]
101     });
102     }
103     catch (e) {
104     console.log(e);
105     }
106 rakin 51
107     await client.logger.logUnmute(user, d);
108     }
109     catch (e) {
110     console.log(e);
111     }
112     }
113    
114     export default class UnmuteCommand extends BaseCommand {
115     supportsInteractions: boolean = true;
116 rakin 203 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
117 rakin 51
118     constructor() {
119     super('unmute', 'moderation', []);
120     }
121    
122     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
123     if (!options.isInteraction && typeof options.args[0] === 'undefined') {
124     await msg.reply({
125     embeds: [
126     new MessageEmbed()
127     .setColor('#f14a60')
128     .setDescription(`This command requires at least one argument.`)
129     ]
130     });
131    
132     return;
133     }
134    
135 rakin 124 if (msg instanceof CommandInteraction)
136     await msg.deferReply();
137    
138 rakin 51 let user: GuildMember;
139    
140     if (options.isInteraction) {
141     user = await <GuildMember> options.options.getMember('member');
142    
143     if (!user) {
144 rakin 124 await this.deferReply(msg, {
145 rakin 51 embeds: [
146     new MessageEmbed()
147     .setColor('#f14a60')
148     .setDescription("Invalid user given.")
149     ]
150     });
151    
152     return;
153     }
154     }
155     else {
156     try {
157     const user2 = await getMember((msg as Message), options);
158    
159     if (!user2) {
160     throw new Error('Invalid user');
161     }
162    
163     user = user2;
164     }
165     catch (e) {
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     console.log(user);
178     }
179    
180 rakin 102 await unmute(client, user, msg.member!.user as User);
181 rakin 51
182 rakin 124 await this.deferReply(msg, {
183 rakin 51 embeds: [
184     new MessageEmbed()
185     .setAuthor({
186     name: user.user.tag,
187     iconURL: user.user.displayAvatarURL(),
188     })
189     .setDescription(user.user.tag + " has been unmuted.")
190     .addFields([
191     {
192     name: "Unmuted by",
193     value: (msg.member!.user as User).tag
194     },
195     ])
196     ]
197     });
198     }
199 rakin 159 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26