2020-05-07 01:26:16 +02:00
|
|
|
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: [
|
2020-05-08 08:50:54 +02:00
|
|
|
// {
|
2020-05-07 01:26:16 +02:00
|
|
|
// name: 'reload',
|
|
|
|
// type: 'STRING',
|
|
|
|
// types: ['FLAG'],
|
|
|
|
// aliases: [ 'r' ],
|
|
|
|
// description: "Reloads the language library",
|
|
|
|
// default: 'all'
|
2020-05-08 08:50:54 +02:00
|
|
|
// }
|
2020-05-21 12:47:58 +02:00
|
|
|
]
|
2020-05-07 01:26:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(message, { params }) {
|
|
|
|
|
|
|
|
//<load|unload|reload|disable|enable> <component>
|
|
|
|
const method = params.shift().toLowerCase();
|
2020-05-21 12:47:58 +02:00
|
|
|
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' });
|
2020-05-07 01:26:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_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' };
|
2020-05-21 12:47:58 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.client.localeLoader.loadLanguage(value);
|
|
|
|
return { msg: `Reloaded locale \`${value}\`` };
|
|
|
|
} catch (err) {
|
|
|
|
return { error: true, msg: err.message };
|
2020-05-07 01:26:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (name === 'all') {
|
|
|
|
|
|
|
|
const errors = [];
|
2020-05-21 12:47:58 +02:00
|
|
|
const { components } = this.client.registry;
|
|
|
|
for (const component of components.values()) {
|
2020-05-07 01:26:16 +02:00
|
|
|
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` };
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
}
|
2020-05-07 01:26:16 +02:00
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
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}` };
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleDisableEnable(name, enable) {
|
|
|
|
|
|
|
|
const component = this.client.registry.components.get(name);
|
2020-05-21 12:47:58 +02:00
|
|
|
let result = null;
|
2020-05-07 01:26:16 +02:00
|
|
|
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}` };
|
2020-05-21 12:47:58 +02:00
|
|
|
return { msg: `Successfully ${enable ? 'enabled' : 'disabled'} component ${name}` };
|
2020-05-07 01:26:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleLoadUnload(name, load) {
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
let result = null;
|
2020-05-07 01:26:16 +02:00
|
|
|
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}` };
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
|
|
|
|
if (result.error) return { error: true, msg: `Cannot ${load ? 'load' : 'unload'} ${name} due to ${result.code}` };
|
2020-05-21 12:47:58 +02:00
|
|
|
return { msg: `Successfully ${load ? 'loaded' : 'unloaded'} component ${name}` };
|
2020-05-07 01:26:16 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ComponentCommand;
|
|
|
|
|
2020-05-21 12:47:58 +02:00
|
|
|
const filterInexact = (search) => (comp) => comp.id.toLowerCase().includes(search) ||
|
2020-05-07 01:26:16 +02:00
|
|
|
comp.resolveable.toLowerCase().includes(search) ||
|
2020-05-21 12:47:58 +02:00
|
|
|
comp.aliases && (comp.aliases.some((ali) => `${comp.type}:${ali}`.toLowerCase().includes(search)) ||
|
|
|
|
comp.aliases.some((ali) => ali.toLowerCase().includes(search)));
|