149 lines
6.0 KiB
JavaScript
149 lines
6.0 KiB
JavaScript
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: [
|
|
// {
|
|
// 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 = null;
|
|
|
|
if (method === 'reload' || method === 'r') 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 message.respond('Invalid method. Can only be `reload`, `enable`, `disable`, `load`, `unload`', 'failure');
|
|
|
|
return 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' };
|
|
}
|
|
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.clone();
|
|
for (const 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` };
|
|
|
|
}
|
|
|
|
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}\`` };
|
|
return { msg: `Successfully reloaded ${name}` };
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_handleDisableEnable(name, enable) {
|
|
|
|
const component = this.client.registry.components.get(name);
|
|
let result = null;
|
|
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}` };
|
|
return { msg: `Successfully ${enable ? 'enabled' : 'disabled'} component ${name}` };
|
|
|
|
}
|
|
|
|
_handleLoadUnload(name, load) {
|
|
|
|
let result = null;
|
|
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}` };
|
|
|
|
}
|
|
const component = this.client.registry.components.filter(filterInexact(name)).first(); //eslint-disable-line no-use-before-define
|
|
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}` };
|
|
return { msg: `Successfully ${load ? 'loaded' : 'unloaded'} component ${name}` };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = ComponentCommand;
|
|
|
|
const filterInexact = (search) => (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))); |