cloudservices/src/commands/load.ts

54 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-11-30 18:47:47 -05:00
import { Message } from 'eris';
2020-06-29 02:50:26 -04:00
import { Client, Command } from '../class';
2019-11-30 18:47:47 -05:00
2019-11-30 18:59:13 -05:00
export default class Load extends Command {
2019-11-30 18:47:47 -05:00
constructor(client: Client) {
super(client);
this.name = 'load';
this.description = '(Re)loads command, config or util';
this.aliases = ['reload'];
2020-04-19 11:35:24 -04:00
this.permissions = { roles: ['662163685439045632'] };
2019-12-16 20:27:12 -05:00
this.enabled = true;
2019-11-30 18:47:47 -05:00
}
public async run(message: Message, args: string[]) {
try {
if (!args[0]) return this.client.commands.get('help').run(message, [this.name]);
const allowed = ['config', 'util', 'command'];
2019-11-30 18:47:47 -05:00
const type = args[0].toLowerCase();
2020-06-29 02:35:14 -04:00
if (!allowed.includes(type)) return this.error(message.channel, 'Invalid type provided to (re)load.');
2019-11-30 18:47:47 -05:00
2020-03-17 01:39:18 -04:00
const corepath = '/opt/CloudServices/dist';
2019-12-16 20:27:12 -05:00
if (type === 'config') {
this.client.config = require(`${corepath}/config.json`);
delete require.cache[`${corepath}/config.json`];
} else if (type === 'util') {
2019-12-16 20:35:16 -05:00
const Util = require(`${corepath}/class/Util`).default;
2019-11-30 18:47:47 -05:00
this.client.util = new Util(this.client);
2019-12-16 20:27:12 -05:00
delete require.cache[`${corepath}/class/Util.js`];
2019-11-30 18:47:47 -05:00
} else {
try {
2019-12-23 18:15:39 -05:00
delete require.cache[`${corepath}/commands/index.js`];
delete require.cache[`${corepath}/commands/${args[1]}.js`];
2019-12-30 11:47:14 -05:00
Object.keys(require.cache).filter((path) => path.includes(`${args[1]}_`)).forEach((path) => delete require.cache[path]);
2020-05-04 08:12:38 -04:00
const cmdIndex = require('.');
2020-06-29 02:35:14 -04:00
let cmd = cmdIndex[args[1]];
if (!cmd) return this.error(message.channel, 'Could not find file.');
cmd = require(`${corepath}/commands/${args[1]}`).default;
2019-11-30 18:47:47 -05:00
this.client.commands.remove(args[1]);
2020-06-29 02:35:14 -04:00
this.client.loadCommand(cmd);
2019-12-16 20:27:12 -05:00
delete require.cache[`${corepath}/commands/index.js`];
delete require.cache[`${corepath}/commands/${args[1]}.js`];
2019-12-30 11:47:14 -05:00
Object.keys(require.cache).filter((path) => path.includes(`${args[1]}_`)).forEach((path) => delete require.cache[path]);
2019-11-30 18:47:47 -05:00
} catch (error) {
2020-06-29 02:35:14 -04:00
if (error.message.includes('Cannot find module')) return this.error(message.channel, 'Could not find file.');
2019-11-30 18:47:47 -05:00
throw error;
}
}
2020-06-29 02:35:14 -04:00
return this.success(message.channel, `Reloaded ${type}.`);
2019-11-30 18:47:47 -05:00
} catch (error) {
return this.client.util.handleError(error, message, this);
}
}
}