-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandsExecutor.ts
66 lines (56 loc) · 2.39 KB
/
CommandsExecutor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Message, MessageEmbed } from 'discord.js';
import CommandSource from '../../structures/commands/CommandSource';
import { Colors, Symbols } from '../../utils/Constants';
import { sendTemporal, canMemberExecute, getMessageOptions } from '../../utils/DiscordUtils';
import { getCommandName } from '../../utils/CommandUtils';
import Event from '../../structures/Event';
import MessageArgumentsParser from '../../MessageArgumentsParser';
import { split } from '../../utils/StringUtils';
export default class CommandsExecutor extends Event {
override async run(message: Message) {
const parsedMessage = getCommandName(message, this.bot.options.prefixes);
const commands = this.bot.commands.getCommands();
if (!parsedMessage.isCommand) return;
const CommandClass = commands.get(parsedMessage.name as string);
if (!CommandClass) return;
const missingPerms = canMemberExecute(message.member, CommandClass.data);
if (!missingPerms.canExecute) {
const errorResponse = new MessageEmbed()
.setTitle(`${Symbols.ERROR} Error`)
.setDescription(`You cannot execute this command: **${missingPerms.reason}**`)
.setColor(Colors.RED);
if (missingPerms.missingPerms?.length) {
errorResponse.addField('Missing permissions', `
\`\`\`
${missingPerms.missingPerms}
\`\`\`
`);
}
await sendTemporal(message, { content: `<@${message.author.id}>`, embeds: [errorResponse] });
return;
}
if (!parsedMessage.prefix || !parsedMessage.name || !parsedMessage.args) return;
const source = new CommandSource(message);
const MessageArgsParser = new MessageArgumentsParser(
source.getRaw() as Message<true>,
split((source.getRaw() as Message<true>).content.substring(parsedMessage.prefix.length + parsedMessage.name.length)),
CommandClass,
CommandClass.getOptions(),
);
const optionResolver = await MessageArgsParser.process();
if (!optionResolver) {
await message.reply(getMessageOptions(CommandClass.data, source, CommandClass.getUsage(parsedMessage.prefix as string)));
return;
}
// FIXME The type is somehow wrong here, CommandClass appears as AbstractCommand, when it's actually a subclass of it.
// @ts-ignore See above
const cmd = new CommandClass(
this.bot,
parsedMessage.name,
source,
parsedMessage.prefix,
optionResolver,
);
await cmd.execute();
}
}