Merge branch 'master' of https://github.com/keanuplayz/TravBot-v3 into experimental

This commit is contained in:
WatDuhHekBro 2020-07-10 15:55:58 -05:00
commit 49d32d4744
5 changed files with 84 additions and 27 deletions

View File

@ -0,0 +1,27 @@
const Event = require('../../Structures/Event');
const panel = require('../../panel.js');
module.exports = class extends Event {
async run(message) {
const mentionRegex = RegExp(`^<@!${this.client.user.id}>$`);
const mentionRegexPrefix = RegExp(`^<@!${this.client.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.client.prefix}\`.`);
const prefix = message.content.match(mentionRegexPrefix) ?
message.content.match(mentionRegexPrefix)[0] : this.client.prefix;
// eslint-disable-next-line no-unused-vars
const [cmd, ...args] = message.content.slice(prefix.length).trim().split(/ +/g);
const command = this.client.commands.get(cmd.toLowerCase()) || this.client.commands.get(this.client.aliases.get(cmd.toLowerCase()));
if (command) {
command.run(message, args);
}
}
};

19
src/Events/ready.js Normal file
View File

@ -0,0 +1,19 @@
const Event = require('../Structures/Event');
module.exports = class extends Event {
constructor(...args) {
super(...args, {
once: true
});
}
run() {
console.log([
`Logged in as ${this.client.user.tag}`,
`Loaded ${this.client.commands.size} commands.`,
`Loaded ${this.client.events.size} events.`
].join('\n'));
}
};

View File

@ -1,9 +1,10 @@
const { Client, Collection } = require('discord.js');
const Util = require('./Util.js');
const panel = require('../panel.js');
module.exports = class BotClient extends Client {
constructor(options = {}, panel) {
constructor(options = {}) {
super({
disableMentions: 'everyone'
});
@ -11,37 +12,14 @@ module.exports = class BotClient extends Client {
this.commands = new Collection();
this.events = 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);
@ -61,7 +39,8 @@ module.exports = class BotClient extends Client {
async start(token = this.token) {
this.utils.loadCommands();
this.utils.loadEvents();
try {
await super.login(token);
} catch (error) {

15
src/Structures/Event.js Normal file
View File

@ -0,0 +1,15 @@
/* eslint-disable no-unused-vars */
module.exports = class Event {
constructor(client, name, options = {}) {
this.name = name;
this.client = client;
this.type = options.once ? 'once' : 'on';
this.emitter = (typeof options.emitter === 'string' ? this.client[options.emitter] : options.emitter) || this.client;
}
async run(...args) {
throw new Error(`The run method has not been implemented in ${this.name}`);
}
};

View File

@ -2,6 +2,7 @@ const path = require('path');
const { promisify } = require('util');
const glob = promisify(require('glob'));
const Command = require('./Command.js');
const Event = require('./Event.js');
module.exports = class Util {
@ -61,4 +62,20 @@ module.exports = class Util {
}
});
}
async loadEvents() {
return glob(`${this.directory}events/**/*.js`).then(events => {
for (const eventFile of events) {
delete require.cache[eventFile];
const { name } = path.parse(eventFile);
const File = require(eventFile);
if (!this.isClass(File)) throw new TypeError(`Event ${name} doesn't export a class!`);
const event = new File(this.client, name.toLowerCase());
if (!(event instanceof Event)) throw new TypeError(`Event ${name} doesn't belong in the Events Directory.`);
this.client.events.set(event.name, event);
event.emitter[event.type](name, (...args) => event.run(...args));
}
});
}
};