1 |
import { BanOptions, CommandInteraction, GuildMember, Interaction, Message, 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 |
import { unmute } from './UnmuteCommand'; |
12 |
|
13 |
export async function mute(client: DiscordClient, dateTime: number | undefined, user: GuildMember, msg: Message | CommandInteraction, timeInterval: number | undefined, reason: string | undefined) { |
14 |
try { |
15 |
if (dateTime) { |
16 |
await client.db.get("INSERT INTO unmutes(user_id, guild_id, time) VALUES(?, ?, ?)", [user.id, msg.guild!.id, new Date(dateTime).toISOString()], async (err: any) => { |
17 |
if (err) |
18 |
console.log(err); |
19 |
|
20 |
console.log('A timeout has been set.'); |
21 |
|
22 |
setTimeout(async () => { |
23 |
await client.db.get("SELECT * FROM unmutes WHERE time = ?", [new Date(dateTime!).toISOString()], async (err: any, data: any) => { |
24 |
if (err) |
25 |
console.log(err); |
26 |
|
27 |
if (data) { |
28 |
await client.db.get('DELETE FROM unmutes WHERE id = ?', [data.id], async (err: any) => { |
29 |
let guild = await client.guilds.cache.find(g => g.id === data.guild_id); |
30 |
let member = await guild?.members.cache.find(m => m.id === data.user_id); |
31 |
|
32 |
if (member) { |
33 |
await unmute(client, member, msg, client.user!); |
34 |
await History.create(member.id, msg.guild!, 'unmute', client.user!.id, null); |
35 |
} |
36 |
|
37 |
console.log(data); |
38 |
}); |
39 |
} |
40 |
}); |
41 |
}, timeInterval); |
42 |
}); |
43 |
} |
44 |
|
45 |
const role = await msg.guild!.roles.fetch(client.config.get('mute_role')); |
46 |
await user.roles.add(role!); |
47 |
await History.create(user.id, msg.guild!, 'mute', msg.member!.user.id, typeof reason === 'undefined' ? null : reason); |
48 |
await client.logger.logMute(user, reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason, timeInterval, msg.member!.user as User); |
49 |
await user.send({ |
50 |
embeds: [ |
51 |
new MessageEmbed() |
52 |
.setAuthor({ |
53 |
iconURL: <string> msg.guild!.iconURL(), |
54 |
name: `\tYou have been muted in ${msg.guild!.name}` |
55 |
}) |
56 |
.addField("Reason", reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason) |
57 |
] |
58 |
}); |
59 |
} |
60 |
catch (e) { |
61 |
console.log(e); |
62 |
|
63 |
await msg.reply({ |
64 |
embeds: [ |
65 |
new MessageEmbed() |
66 |
.setColor('#f14a60') |
67 |
.setDescription("Failed to assign the muted role to this user. Maybe missing permisions/roles or I'm not allowed to assign roles this user?") |
68 |
] |
69 |
}); |
70 |
|
71 |
return; |
72 |
} |
73 |
} |
74 |
|
75 |
export default class MuteCommand extends BaseCommand { |
76 |
supportsInteractions: boolean = true; |
77 |
|
78 |
constructor() { |
79 |
super('mute', 'moderation', []); |
80 |
} |
81 |
|
82 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
83 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
84 |
await msg.reply({ |
85 |
embeds: [ |
86 |
new MessageEmbed() |
87 |
.setColor('#f14a60') |
88 |
.setDescription(`This command requires at least one argument.`) |
89 |
] |
90 |
}); |
91 |
|
92 |
return; |
93 |
} |
94 |
|
95 |
let user: GuildMember; |
96 |
let reason: string | undefined; |
97 |
let time: string | undefined; |
98 |
let timeInterval: number | undefined; |
99 |
let dateTime: number | undefined; |
100 |
|
101 |
if (options.isInteraction) { |
102 |
user = await <GuildMember> options.options.getMember('member'); |
103 |
|
104 |
if (options.options.getString('reason')) { |
105 |
reason = await <string> options.options.getString('reason'); |
106 |
} |
107 |
|
108 |
if (options.options.getString('time')) { |
109 |
time = await options.options.getString('time') as string; |
110 |
timeInterval = await ms(time); |
111 |
|
112 |
if (!timeInterval) { |
113 |
await msg.reply({ |
114 |
embeds: [ |
115 |
new MessageEmbed() |
116 |
.setColor('#f14a60') |
117 |
.setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`) |
118 |
] |
119 |
}); |
120 |
|
121 |
return; |
122 |
} |
123 |
} |
124 |
} |
125 |
else { |
126 |
const user2 = await getMember((msg as Message), options); |
127 |
|
128 |
if (!user2) { |
129 |
await msg.reply({ |
130 |
embeds: [ |
131 |
new MessageEmbed() |
132 |
.setColor('#f14a60') |
133 |
.setDescription(`Invalid user given.`) |
134 |
] |
135 |
}); |
136 |
|
137 |
return; |
138 |
} |
139 |
|
140 |
user = user2; |
141 |
|
142 |
const index = await options.args.indexOf('-t'); |
143 |
|
144 |
if (options.args[1]) { |
145 |
const args = [...options.args]; |
146 |
args.shift(); |
147 |
|
148 |
if (index !== -1) { |
149 |
args.splice(index - 1, 2) |
150 |
} |
151 |
|
152 |
reason = await args.join(' '); |
153 |
} |
154 |
|
155 |
if (index !== -1) { |
156 |
time = await options.args[index + 1]; |
157 |
|
158 |
if (time === undefined) { |
159 |
await msg.reply({ |
160 |
embeds: [ |
161 |
new MessageEmbed() |
162 |
.setColor('#f14a60') |
163 |
.setDescription(`Option \`-t\` (time) requires an argument.`) |
164 |
] |
165 |
}); |
166 |
|
167 |
return; |
168 |
} |
169 |
|
170 |
if (!ms(time)) { |
171 |
await msg.reply({ |
172 |
embeds: [ |
173 |
new MessageEmbed() |
174 |
.setColor('#f14a60') |
175 |
.setDescription(`Option \`-t\` (time) requires an argument which must be a valid time interval.`) |
176 |
] |
177 |
}); |
178 |
|
179 |
return; |
180 |
} |
181 |
|
182 |
timeInterval = await ms(time); |
183 |
} |
184 |
} |
185 |
|
186 |
if (timeInterval) { |
187 |
dateTime = Date.now() + timeInterval; |
188 |
} |
189 |
|
190 |
await mute(client, dateTime, user, msg, timeInterval, reason); |
191 |
|
192 |
const fields = [ |
193 |
{ |
194 |
name: "Muted by", |
195 |
value: (msg.member!.user as User).tag |
196 |
}, |
197 |
{ |
198 |
name: "Reason", |
199 |
value: reason === undefined || reason.trim() === '' ? "*No reason provided*" : reason |
200 |
}, |
201 |
{ |
202 |
name: "Duration", |
203 |
value: time === undefined ? "*No duration set*" : (time + '') |
204 |
} |
205 |
]; |
206 |
|
207 |
console.log(fields); |
208 |
|
209 |
await msg.reply({ |
210 |
embeds: [ |
211 |
new MessageEmbed() |
212 |
.setAuthor({ |
213 |
name: user.user.tag, |
214 |
iconURL: user.displayAvatarURL(), |
215 |
}) |
216 |
.setDescription(user.user.tag + " has been muted.") |
217 |
.addFields(fields) |
218 |
] |
219 |
}); |
220 |
} |
221 |
} |