community-relations/src/events/CommandHandler.ts

33 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-04-14 13:15:33 -04:00
/* eslint-disable no-useless-return */
2020-04-17 08:42:25 -04:00
import { Message, TextChannel, NewsChannel } from 'eris';
import { Client, Event } from '../class';
2020-04-14 13:15:33 -04:00
export default class CommandHandler extends Event {
2020-04-14 13:15:33 -04:00
public client: Client;
constructor(client: Client) {
super(client);
this.event = 'messageCreate';
2020-04-14 13:15:33 -04:00
}
public async run(message: Message) {
try {
if (message.author.bot) return;
if (message.content.indexOf(this.client.config.prefix) !== 0) return;
const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g);
const resolved = await this.client.util.resolveCommand(noPrefix);
if (!resolved) return;
2020-04-17 08:42:25 -04:00
if (resolved.cmd.guildOnly && !(message.channel instanceof TextChannel || message.channel instanceof NewsChannel)) return;
if (!resolved.cmd.enabled) { message.channel.createMessage(`***${this.client.util.emojis.ERROR} This command has been disabled***`); return; }
2020-04-15 15:12:56 -04:00
if (!resolved.cmd.checkPermissions(message.member)) return;
2020-05-06 19:33:27 -04:00
if ((message.channel.type === 0) && !message.channel.guild.members.get(message.author.id)) {
message.channel.guild.members.add(await message.channel.guild.getRESTMember(message.author.id));
}
this.client.util.signale.log(`User '${message.author.username}#${message.author.discriminator}' ran command '${resolved.cmd.name}' in '${message.channel.id}'.`);
await resolved.cmd.run(message, resolved.args);
} catch (err) {
this.client.util.handleError(err, message);
}
2020-04-14 13:15:33 -04:00
}
}