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-17 05:00:41 +00:00
|
|
|
async unload (name, category) {
|
|
|
|
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-08 01:55:39 +00:00
|
|
|
|
2020-10-17 05:00:41 +00:00
|
|
|
if (!command) {
|
|
|
|
return `\`${name}\` does not exist as a command or an alias.`;
|
|
|
|
}
|
2020-10-08 01:55:39 +00:00
|
|
|
|
2020-10-17 05:00:41 +00:00
|
|
|
this.client.logger.debug(`Unloading command ${category}/${name}`);
|
|
|
|
|
|
|
|
if (command.shutdown) {
|
|
|
|
await command.shutdown(this.client);
|
2020-10-08 01:55:39 +00:00
|
|
|
}
|
2020-10-17 05:00:41 +00:00
|
|
|
delete require.cache[require.resolve(path)];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
const resp = this.unload(cmd, dir);
|
|
|
|
if (resp) {
|
|
|
|
this.client.logger.error(resp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
unload (name) { //eslint-disable-line no-unused-vars
|
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-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
|
|
|
};
|