TravBot-v3/src/Structures/BotClient.js

74 lines
2.1 KiB
JavaScript

const { Client, Collection } = require('discord.js');
const Util = require('./Util.js');
module.exports = class BotClient extends Client {
constructor(options = {}, panel) {
super({
disableMentions: 'everyone'
});
this.validate(options);
this.commands = new Collection();
this.aliases = new Collection();
this.utils = new Util(this);
this.owners = options.owners;
this.once('ready', () => {
console.log(`Logged in as ${this.user.username}.`);
});
this.on('message', async (message) => {
const mentionRegex = RegExp(`^<@!${this.user.id}>$`);
const mentionRegexPrefix = RegExp(`^<@!${this.user.id}> `);
if (!message.guild || message.author.bot) return;
panel.send(message);
if (message.content.match(mentionRegex)) message.channel.send(`My prefix for ${message.guild.name} is \`${this.prefix}\`.`);
const prefix = message.content.match(mentionRegexPrefix) ?
message.content.match(mentionRegexPrefix)[0] : this.prefix;
// eslint-disable-next-line no-unused-vars
const [cmd, ...args] = message.content.slice(prefix.length).trim().split(/ +/g);
const command = this.commands.get(cmd.toLowerCase()) || this.commands.get(this.aliases.get(cmd.toLowerCase()));
if (command) {
command.run(message, args);
}
});
panel.listen('message', info => {
if (!info || !('channel' in info) || !('message' in info)) return;
this.channels.cache.get(info.channel).send(info.message);
});
}
validate(options) {
if (typeof options !== 'object') throw new TypeError('Options should be a type of Object.');
if (!options.token) throw new Error('You must pass a token for the client.');
this.token = options.token;
if (!options.prefix) throw new Error('You must pass a prefix for the client.');
if (typeof options.prefix !== 'string') throw new TypeError('Prefix should be a type of String.');
this.prefix = options.prefix;
}
async start(token = this.token) {
this.utils.loadCommands();
try {
await super.login(token);
} catch (error) {
console.error('It seems that the token you provided is invalid.');
require('../setup.js').again();
}
}
};