/[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 336 - (show 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 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 try {
20 await user.roles.remove(role!, 'Unmuting user');
21 }
22 catch (e) {
23 console.log(e);
24 }
25
26 const { default: Punishment } = await import('../../models/Punishment');
27
28 const { getTimeouts, clearTimeoutv2 } = await import('../../utils/setTimeout');
29
30 const { default: Hardmute } = await import("../../models/Hardmute");
31 const { default: MuteRecord } = await import("../../models/MuteRecord");
32
33 const hardmute = await Hardmute.findOne({
34 user_id: user.id,
35 guild_id: user.guild.id
36 });
37
38 if (hardmute) {
39 for await (const roleID of hardmute.roles) {
40 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 await hardmute.delete();
53 }
54
55 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 if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
64 await clearTimeoutv2(timeout);
65 }
66 }
67 }
68 catch (e) {
69 console.log(e);
70 }
71 }
72 }
73
74 await Punishment.create({
75 type: PunishmentType.UNMUTE,
76 user_id: user.id,
77 guild_id: user.guild!.id,
78 mod_id: d.id,
79 mod_tag: d.tag,
80 createdAt: new Date()
81 });
82
83 const muteRecord = await MuteRecord.findOne({
84 user_id: user.user.id,
85 guild_id: user.guild.id
86 });
87
88 if (muteRecord) {
89 await muteRecord.delete();
90 }
91
92 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
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 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
117
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 if (msg instanceof CommandInteraction)
136 await msg.deferReply();
137
138 let user: GuildMember;
139
140 if (options.isInteraction) {
141 user = await <GuildMember> options.options.getMember('member');
142
143 if (!user) {
144 await this.deferReply(msg, {
145 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 await this.deferReply(msg, {
167 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 await unmute(client, user, msg.member!.user as User);
181
182 await this.deferReply(msg, {
183 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 }

team@onesoftnet.eu.org
ViewVC Help
Powered by ViewVC 1.1.26