/[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 203 - (show annotations)
Mon Jul 29 17:29:00 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5801 byte(s)
refactor: mute command requires MODERATE_MEMBERS permission
1 import { BanOptions, CommandInteraction, Guild, GuildMember, Interaction, Message, Permissions, 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 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
106
107 constructor() {
108 super('unmute', 'moderation', []);
109 }
110
111 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
112 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
113 await msg.reply({
114 embeds: [
115 new MessageEmbed()
116 .setColor('#f14a60')
117 .setDescription(`This command requires at least one argument.`)
118 ]
119 });
120
121 return;
122 }
123
124 if (msg instanceof CommandInteraction)
125 await msg.deferReply();
126
127 let user: GuildMember;
128
129 if (options.isInteraction) {
130 user = await <GuildMember> options.options.getMember('member');
131
132 if (!user) {
133 await this.deferReply(msg, {
134 embeds: [
135 new MessageEmbed()
136 .setColor('#f14a60')
137 .setDescription("Invalid user given.")
138 ]
139 });
140
141 return;
142 }
143 }
144 else {
145 try {
146 const user2 = await getMember((msg as Message), options);
147
148 if (!user2) {
149 throw new Error('Invalid user');
150 }
151
152 user = user2;
153 }
154 catch (e) {
155 await this.deferReply(msg, {
156 embeds: [
157 new MessageEmbed()
158 .setColor('#f14a60')
159 .setDescription(`Invalid user given.`)
160 ]
161 });
162
163 return;
164 }
165
166 console.log(user);
167 }
168
169 await unmute(client, user, msg.member!.user as User);
170
171 await this.deferReply(msg, {
172 embeds: [
173 new MessageEmbed()
174 .setAuthor({
175 name: user.user.tag,
176 iconURL: user.user.displayAvatarURL(),
177 })
178 .setDescription(user.user.tag + " has been unmuted.")
179 .addFields([
180 {
181 name: "Unmuted by",
182 value: (msg.member!.user as User).tag
183 },
184 ])
185 ]
186 });
187 }
188 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26