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