Implemented an event handler.

Also fixed Uptime's super.
This commit is contained in:
Keanu Timmermans 2020-07-08 12:04:19 +02:00
parent 21a01aa188
commit d7f2232dc0
6 changed files with 80 additions and 25 deletions

View File

@ -4,7 +4,7 @@ const ms = require('ms');
module.exports = class extends Command {
constructor(...args) {
super(...args)({
super(...args, {
aliases: ['uptime'],
category: 'Utilities'
});

View File

@ -0,0 +1,25 @@
const Event = require('../../Structures/Event');
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;
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

@ -11,35 +11,13 @@ 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;
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);
}
});
}
validate(options) {
@ -55,6 +33,7 @@ module.exports = class BotClient extends Client {
async start(token = this.token) {
this.utils.loadCommands();
this.utils.loadEvents();
super.login(token);
}

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));
}
});
}
};