/[sudobot]/trunk/src/commands/moderation/UnmuteCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/moderation/UnmuteCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26