/[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 124 - (show annotations)
Mon Jul 29 17:28:41 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5661 byte(s)
Added hardmuting support
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 await user.send({
82 embeds: [
83 new MessageEmbed()
84 .setAuthor({
85 iconURL: <string> user.guild!.iconURL(),
86 name: `\tYou have been unmuted in ${user.guild!.name}`
87 })
88 ]
89 });
90
91 await client.logger.logUnmute(user, d);
92 }
93 catch (e) {
94 console.log(e);
95 }
96 }
97
98 export default class UnmuteCommand extends BaseCommand {
99 supportsInteractions: boolean = true;
100
101 constructor() {
102 super('unmute', 'moderation', []);
103 }
104
105 async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
106 if (!options.isInteraction && typeof options.args[0] === 'undefined') {
107 await msg.reply({
108 embeds: [
109 new MessageEmbed()
110 .setColor('#f14a60')
111 .setDescription(`This command requires at least one argument.`)
112 ]
113 });
114
115 return;
116 }
117
118 if (msg instanceof CommandInteraction)
119 await msg.deferReply();
120
121 let user: GuildMember;
122
123 if (options.isInteraction) {
124 user = await <GuildMember> options.options.getMember('member');
125
126 if (!user) {
127 await this.deferReply(msg, {
128 embeds: [
129 new MessageEmbed()
130 .setColor('#f14a60')
131 .setDescription("Invalid user given.")
132 ]
133 });
134
135 return;
136 }
137 }
138 else {
139 try {
140 const user2 = await getMember((msg as Message), options);
141
142 if (!user2) {
143 throw new Error('Invalid user');
144 }
145
146 user = user2;
147 }
148 catch (e) {
149 await this.deferReply(msg, {
150 embeds: [
151 new MessageEmbed()
152 .setColor('#f14a60')
153 .setDescription(`Invalid user given.`)
154 ]
155 });
156
157 return;
158 }
159
160 console.log(user);
161 }
162
163 await unmute(client, user, msg.member!.user as User);
164
165 await this.deferReply(msg, {
166 embeds: [
167 new MessageEmbed()
168 .setAuthor({
169 name: user.user.tag,
170 iconURL: user.user.displayAvatarURL(),
171 })
172 .setDescription(user.user.tag + " has been unmuted.")
173 .addFields([
174 {
175 name: "Unmuted by",
176 value: (msg.member!.user as User).tag
177 },
178 ])
179 ]
180 });
181 }
182 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26