galactic-bot/structure/client/components/commands/developer/Component.js

158 lines
6.1 KiB
JavaScript
Raw Normal View History

const { Command } = require('../../../../interfaces/');
const path = require('path');
const fs = require('fs');
class ComponentCommand extends Command {
constructor(client) {
super(client, {
name: 'component',
module: 'developer',
restricted: true,
aliases: [
'c',
'comp'
],
usage: '[component..]',
arguments: [
// new Argument(client, {
// name: 'reload',
// type: 'STRING',
// types: ['FLAG'],
// aliases: [ 'r' ],
// description: "Reloads the language library",
// default: 'all'
// })
],
});
this.client = client;
}
async execute(message, { params }) {
//<load|unload|reload|disable|enable> <component>
const method = params.shift().toLowerCase();
let response;
if (method === 'reload')
response = this._handleReload(params);
else if (method === 'enable')
response = this._handleDisableEnable(params.shift().toLowerCase(), true);
else if (method === 'disable')
response = this._handleDisableEnable(params.shift().toLowerCase(), false);
else if (method === 'load')
response = this._handleLoadUnload(params.shift().toLowerCase(), true);
else if (method === 'unload')
response = this._handleLoadUnload(params.shift().toLowerCase(), false);
else return await message.respond('Invalid method. Can only be `reload`, `enable`, `disable`, `load`, `unload`', 'failure');
return await message.respond(response.msg, { emoji: response.error ? 'failure' : 'success' });
}
_handleReload(params) {
const name = params.length ? params.shift().toLowerCase() : 'all'; //ex. language
const value = params.length ? params.shift().toLowerCase() : 'all'; //ex. en_us --> combined: -component reload language en_us
if (name === 'language' || name === 'lang' || name === 'l') {
if (value === 'all') {
this.client.localeLoader.loadLanguages();
return { msg: 'Reloaded all languages' };
} else {
try {
this.client.localeLoader.loadLanguage(value);
return { msg: `Reloaded locale \`${value}\`` };
} catch (err) {
return { error: true, msg: err.message };
}
}
} else {
if (name === 'all') {
const errors = [];
const components = this.client.registry.components;
for (let component of components.values()) {
const result = component.reload();
if (result.error) errors.push(`Component ${component.id} errored while reloading with code \`${result.code}\``);
}
if (errors.length) return { error: true, msg: `The following errors occurred during reload:\n${errors.join('\n')}` };
return { msg: `Successfully reloaded all components` };
} else {
const component = this.client.registry.components.get(name);
if (!component) return { error: true, msg: `Component ${name} doesn't exist.` };
const result = component.reload();
if (result.error) return { error: true, msg: `Component ${name} errored while reloading with code \`${result.code}\`` };
else return { msg: `Successfully reloaded ${name}` };
}
}
}
_handleDisableEnable(name, enable) {
const component = this.client.registry.components.get(name);
let result;
if (!component) return { error: true, msg: `Component ${name} doesn't exist.` };
if (enable) result = component.enable();
else result = component.disable();
if (result.error) return { error: true, msg: `Cannot ${enable ? 'enable' : 'disable'} ${name} due to ${result.code}` };
else return { msg: `Successfully ${enable ? 'enabled' : 'disabled'} component ${name}` };
}
_handleLoadUnload(name, load) {
let result;
if (load) {
const directory = path.join(process.cwd(), 'structure/client/components', name);
try {
fs.accessSync(directory);
} catch(err) {
return { error: true, msg: `\`${name}\` is an invalid path!` };
}
// components/commands/utility/Ping.js
const func = require(directory); //directory
if (typeof func !== 'function') {
delete require.cache[directory];
return { error: true, msg: 'Attempted to index an invalid function as a component.' };
}
const component = new func(this.client);
result = this.client.registry.loadComponent(component, directory);
if (!result) return { error: true, msg: `Failed to load component ${name}, see console.` };
return { msg: `Successfully loaded component: ${component.resolveable}` };
} else {
const component = this.client.registry.components.filter(filterInexact(name)).first();
if (!component) return { error: true, msg: `Component ${name} doesn't exist.` };
result = component.unload();
}
if (result.error) return { error: true, msg: `Cannot ${load ? 'load' : 'unload'} ${name} due to ${result.code}` };
else return { msg: `Successfully ${load ? 'loaded' : 'unloaded'} component ${name}` };
}
}
module.exports = ComponentCommand;
const filterInexact = (search) => {
return comp => comp.id.toLowerCase().includes(search) ||
comp.resolveable.toLowerCase().includes(search) ||
(comp.aliases && (comp.aliases.some(ali => `${comp.type}:${ali}`.toLowerCase().includes(search)) ||
comp.aliases.some(ali => ali.toLowerCase().includes(search))));
};