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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 227 - (hide 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 rakin 203 import { BanOptions, CommandInteraction, Guild, GuildMember, Interaction, Message, Permissions, User } from 'discord.js';
2 rakin 51 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 rakin 86 import PunishmentType from '../../types/PunishmentType';
13    
14 rakin 102 export async function unmute(client: DiscordClient, user: GuildMember, d: User) {
15 rakin 51 try {
16 rakin 102 await History.create(user.id, user.guild!, 'unmute', d.id, null);
17 rakin 51
18 rakin 106 const role = await user.guild!.roles.fetch(client.config.props[user.guild.id].mute_role);
19 rakin 124 await user.roles.remove(role!, 'Unmuting user');
20 rakin 51
21 rakin 86 const { default: Punishment } = await import('../../models/Punishment');
22    
23 rakin 102 const { getTimeouts, clearTimeoutv2 } = await import('../../utils/setTimeout');
24    
25 rakin 124 const { default: Hardmute } = await import("../../models/Hardmute");
26 rakin 227 const { default: MuteRecord } = await import("../../models/MuteRecord");
27 rakin 124
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 rakin 102 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 rakin 106 if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) {
64 rakin 102 await clearTimeoutv2(timeout);
65     }
66     }
67     }
68     catch (e) {
69     console.log(e);
70     }
71     }
72     }
73    
74 rakin 86 await Punishment.create({
75     type: PunishmentType.UNMUTE,
76     user_id: user.id,
77 rakin 102 guild_id: user.guild!.id,
78 rakin 86 mod_id: d.id,
79     mod_tag: d.tag,
80     });
81    
82 rakin 227 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 rakin 159 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 rakin 51
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 rakin 203 permissions = [Permissions.FLAGS.MODERATE_MEMBERS];
118 rakin 51
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 rakin 124 if (msg instanceof CommandInteraction)
137     await msg.deferReply();
138    
139 rakin 51 let user: GuildMember;
140    
141     if (options.isInteraction) {
142     user = await <GuildMember> options.options.getMember('member');
143    
144     if (!user) {
145 rakin 124 await this.deferReply(msg, {
146 rakin 51 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 rakin 124 await this.deferReply(msg, {
168 rakin 51 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 rakin 102 await unmute(client, user, msg.member!.user as User);
182 rakin 51
183 rakin 124 await this.deferReply(msg, {
184 rakin 51 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 rakin 159 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26