1 |
import { CommandInteraction, GuildMember, 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 getMember from '../../utils/getMember'; |
8 |
|
9 |
import PunishmentType from '../../types/PunishmentType'; |
10 |
|
11 |
export async function unmute(client: DiscordClient, user: GuildMember, d: User) { |
12 |
try { |
13 |
const role = await user.guild!.roles.fetch(client.config.props[user.guild.id].mute_role); |
14 |
try { |
15 |
await user.roles.remove(role!, 'Unmuting user'); |
16 |
} |
17 |
catch (e) { |
18 |
console.log(e); |
19 |
} |
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 |
user_id: user.id, |
30 |
guild_id: user.guild.id |
31 |
}); |
32 |
|
33 |
if (hardmute) { |
34 |
for await (const roleID of hardmute.roles) { |
35 |
try { |
36 |
const role = await user.guild.roles.fetch(roleID); |
37 |
|
38 |
if (role) { |
39 |
await user.roles.add(role, 'Adding the roles which were removed due to hardmute'); |
40 |
} |
41 |
} |
42 |
catch (e) { |
43 |
console.log(e); |
44 |
} |
45 |
} |
46 |
|
47 |
await hardmute.delete(); |
48 |
} |
49 |
|
50 |
const timeouts = getTimeouts(); |
51 |
|
52 |
for (const timeout of timeouts.values()) { |
53 |
if (timeout.row.params) { |
54 |
try { |
55 |
const json = JSON.parse(timeout.row.params); |
56 |
|
57 |
if (json) { |
58 |
if (json[1] === user.id && timeout.row.filePath.endsWith('unmute-job')) { |
59 |
await clearTimeoutv2(timeout); |
60 |
} |
61 |
} |
62 |
} |
63 |
catch (e) { |
64 |
console.log(e); |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
await Punishment.create({ |
70 |
type: PunishmentType.UNMUTE, |
71 |
user_id: user.id, |
72 |
guild_id: user.guild!.id, |
73 |
mod_id: d.id, |
74 |
mod_tag: d.tag, |
75 |
createdAt: new Date() |
76 |
}); |
77 |
|
78 |
const muteRecord = await MuteRecord.findOne({ |
79 |
user_id: user.user.id, |
80 |
guild_id: user.guild.id |
81 |
}); |
82 |
|
83 |
if (muteRecord) { |
84 |
await muteRecord.delete(); |
85 |
} |
86 |
|
87 |
try { |
88 |
await user.send({ |
89 |
embeds: [ |
90 |
new MessageEmbed() |
91 |
.setAuthor({ |
92 |
iconURL: <string> user.guild!.iconURL(), |
93 |
name: `\tYou have been unmuted in ${user.guild!.name}` |
94 |
}) |
95 |
] |
96 |
}); |
97 |
} |
98 |
catch (e) { |
99 |
console.log(e); |
100 |
} |
101 |
|
102 |
await client.logger.logUnmute(user, d); |
103 |
} |
104 |
catch (e) { |
105 |
console.log(e); |
106 |
} |
107 |
} |
108 |
|
109 |
export default class UnmuteCommand extends BaseCommand { |
110 |
supportsInteractions: boolean = true; |
111 |
permissions = [Permissions.FLAGS.MODERATE_MEMBERS]; |
112 |
|
113 |
constructor() { |
114 |
super('unmute', 'moderation', []); |
115 |
} |
116 |
|
117 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
118 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
119 |
await msg.reply({ |
120 |
embeds: [ |
121 |
new MessageEmbed() |
122 |
.setColor('#f14a60') |
123 |
.setDescription(`This command requires at least one argument.`) |
124 |
] |
125 |
}); |
126 |
|
127 |
return; |
128 |
} |
129 |
|
130 |
if (msg instanceof CommandInteraction) |
131 |
await msg.deferReply(); |
132 |
|
133 |
let user: GuildMember; |
134 |
|
135 |
if (options.isInteraction) { |
136 |
user = await <GuildMember> options.options.getMember('member'); |
137 |
|
138 |
if (!user) { |
139 |
await this.deferReply(msg, { |
140 |
embeds: [ |
141 |
new MessageEmbed() |
142 |
.setColor('#f14a60') |
143 |
.setDescription("Invalid user given.") |
144 |
] |
145 |
}); |
146 |
|
147 |
return; |
148 |
} |
149 |
} |
150 |
else { |
151 |
try { |
152 |
const user2 = await getMember((msg as Message), options); |
153 |
|
154 |
if (!user2) { |
155 |
throw new Error('Invalid user'); |
156 |
} |
157 |
|
158 |
user = user2; |
159 |
} |
160 |
catch (e) { |
161 |
await this.deferReply(msg, { |
162 |
embeds: [ |
163 |
new MessageEmbed() |
164 |
.setColor('#f14a60') |
165 |
.setDescription(`Invalid user given.`) |
166 |
] |
167 |
}); |
168 |
|
169 |
return; |
170 |
} |
171 |
|
172 |
console.log(user); |
173 |
} |
174 |
|
175 |
await unmute(client, user, msg.member!.user as User); |
176 |
|
177 |
await this.deferReply(msg, { |
178 |
embeds: [ |
179 |
new MessageEmbed() |
180 |
.setAuthor({ |
181 |
name: user.user.tag, |
182 |
iconURL: user.user.displayAvatarURL(), |
183 |
}) |
184 |
.setDescription(user.user.tag + " has been unmuted.") |
185 |
.addFields([ |
186 |
{ |
187 |
name: "Unmuted by", |
188 |
value: (msg.member!.user as User).tag |
189 |
}, |
190 |
]) |
191 |
] |
192 |
}); |
193 |
} |
194 |
} |