diff --git a/src/Events/message/message.js b/src/Events/message/message.js new file mode 100644 index 0000000..44e88fa --- /dev/null +++ b/src/Events/message/message.js @@ -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); + } + } + +}; 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 28fd80d..6ab53e2 100644 --- a/src/Structures/BotClient.js +++ b/src/Structures/BotClient.js @@ -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) { 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)); + } + }); + } + };