diff --git a/src/Commands/Utilities/Uptime.js b/src/Commands/Utilities/Uptime.js index b782654..5608c44 100644 --- a/src/Commands/Utilities/Uptime.js +++ b/src/Commands/Utilities/Uptime.js @@ -4,7 +4,7 @@ const ms = require('ms'); module.exports = class extends Command { constructor(...args) { - super(...args)({ + super(...args, { aliases: ['uptime'], category: 'Utilities' }); diff --git a/src/Events/message/message.js b/src/Events/message/message.js new file mode 100644 index 0000000..c2a23b8 --- /dev/null +++ b/src/Events/message/message.js @@ -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); + } + } + +}; diff --git a/src/Events/ready.js b/src/Events/ready.js new file mode 100644 index 0000000..16dfe9f --- /dev/null +++ b/src/Events/ready.js @@ -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')); + } + +}; diff --git a/src/Structures/BotClient.js b/src/Structures/BotClient.js index 4d5568e..51a8326 100644 --- a/src/Structures/BotClient.js +++ b/src/Structures/BotClient.js @@ -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); } diff --git a/src/Structures/Event.js b/src/Structures/Event.js new file mode 100644 index 0000000..5c816a1 --- /dev/null +++ b/src/Structures/Event.js @@ -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}`); + } + +}; diff --git a/src/Structures/Util.js b/src/Structures/Util.js index 39d9ee4..40045cd 100644 --- a/src/Structures/Util.js +++ b/src/Structures/Util.js @@ -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)); + } + }); + } + };