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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26