woomy-v2/bot/util/handlers.js

126 lines
4 KiB
JavaScript
Raw Normal View History

2020-10-17 05:00:41 +00:00
const fs = require('fs');
2020-08-18 04:58:36 +00:00
class CommandHandler {
2020-10-17 05:00:41 +00:00
constructor (client) {
this.client = client;
}
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
load (name, category) {
try {
const path = this.client.path + '/commands/' + category + '/' + name + '.js';
const props = new (require(path))(this.client);
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
this.client.logger.debug(`Loading command ${category}/${name}`);
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
props.help.name = name;
props.help.category = category;
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
if (props.init) {
props.init(this.client);
}
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
this.client.commands.set(props.help.name, props);
this.client.cooldowns.set(props.help.name, new Map());
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
props.conf.aliases.forEach(alias => {
this.client.aliases.set(alias, props.help.name);
});
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
return;
} catch (err) {
return `Failed to load command ${name}: ${err.stack}`;
2020-08-18 04:58:36 +00:00
}
2020-10-17 05:00:41 +00:00
}
2020-10-06 07:50:18 +00:00
2020-10-17 05:00:41 +00:00
loadAll () {
const commandDirectories = fs.readdirSync('./commands/');
this.client.logger.debug(`Found ${commandDirectories.length} command directories.`);
commandDirectories.forEach((dir) => {
const commandFiles = fs.readdirSync('./commands/' + dir + '/');
commandFiles.filter((cmd) => cmd.split('.').pop() === 'js').forEach((cmd) => {
cmd = cmd.substring(0, cmd.length - 3);
const resp = this.load(cmd, dir);
if (resp) {
this.client.logger.error(resp);
}
});
});
this.client.logger.info(`Loaded a total of ${this.client.commands.size} commands.`);
}
2020-10-06 07:50:18 +00:00
2020-10-18 02:10:11 +00:00
unload (name, category) {
2020-10-17 05:00:41 +00:00
const path = this.client.path + '/commands/' + category + '/' + name + '.js';
2020-10-06 07:50:18 +00:00
2020-10-17 05:00:41 +00:00
let command;
2020-10-06 07:50:18 +00:00
2020-10-17 05:00:41 +00:00
if (this.client.commands.has(name)) {
command = this.client.commands.get(name);
} else if (this.client.aliases.has(name)) {
command = this.client.commands.get(this.client.aliases.get(name));
}
2020-10-18 01:41:55 +00:00
if (!command) return `\`${name}\` does not exist as a command or an alias.`;
2020-10-17 05:00:41 +00:00
this.client.logger.debug(`Unloading command ${category}/${name}`);
2020-10-18 01:41:55 +00:00
for (const alias of command.conf.aliases) this.client.aliases.delete(alias);
this.client.commands.delete(command.help.name);
2020-10-17 05:00:41 +00:00
delete require.cache[require.resolve(path)];
2020-10-18 01:41:55 +00:00
return;
2020-10-17 05:00:41 +00:00
}
unloadAll () {
const commandDirectories = fs.readdirSync('./commands/');
this.client.logger.debug(`Found ${commandDirectories.length} command directories.`);
commandDirectories.forEach((dir) => {
const commandFiles = fs.readdirSync('./commands/' + dir + '/');
commandFiles.filter((cmd) => cmd.split('.').pop() === 'js').forEach((cmd) => {
cmd = cmd.substring(0, cmd.length - 3);
2020-10-18 02:10:11 +00:00
const res = this.unload(cmd, dir);
2020-10-18 02:51:29 +00:00
if (res) this.client.logger.error('Command unload: ' + res);
2020-10-17 05:00:41 +00:00
});
});
}
2020-08-18 04:58:36 +00:00
}
class EventHandler {
2020-10-17 05:00:41 +00:00
constructor (client) {
this.client = client;
2020-08-18 04:58:36 +00:00
}
2020-10-17 05:00:41 +00:00
load (name) {
try {
this.client.logger.debug(`Loading event ${name}`);
const path = this.client.path + '/events/' + name + '.js';
const event = new (require(path))(this.client);
this.client.on(name, (...args) => event.run(...args));
delete require.cache[require.resolve(path)];
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
return;
} catch (err) {
return `Failed to load event ${name}: ${err}`;
}
}
2020-08-18 04:58:36 +00:00
2020-10-17 05:00:41 +00:00
loadAll () {
const eventFiles = fs.readdirSync(this.client.path + '/events');
eventFiles.forEach(file => {
const name = file.split('.')[0];
const resp = this.load(name);
if (resp) {
this.client.logger.error(resp);
}
});
}
2020-10-18 02:57:31 +00:00
// TO-DO: EVENT UNLOADING/RELOADING
2020-08-18 04:58:36 +00:00
}
module.exports = {
2020-10-17 05:00:41 +00:00
CommandHandler: CommandHandler,
EventHandler: EventHandler
2020-08-18 04:58:36 +00:00
};