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