cloudservices/src/commands/load.ts

54 lines
2.3 KiB
TypeScript

import { Message } from 'eris';
import { Client, Command } from '../class';
export default class Load extends Command {
constructor(client: Client) {
super(client);
this.name = 'load';
this.description = '(Re)loads command, config or util';
this.aliases = ['reload'];
this.permissions = { roles: ['662163685439045632'] };
this.enabled = true;
}
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'];
const type = args[0].toLowerCase();
if (!allowed.includes(type)) return this.error(message.channel, 'Invalid type provided to (re)load.');
const corepath = '/opt/CloudServices/dist';
if (type === 'config') {
this.client.config = require(`${corepath}/config.json`);
delete require.cache[`${corepath}/config.json`];
} else if (type === 'util') {
const Util = require(`${corepath}/class/Util`).default;
this.client.util = new Util(this.client);
delete require.cache[`${corepath}/class/Util.js`];
} else {
try {
delete require.cache[`${corepath}/commands/index.js`];
delete require.cache[`${corepath}/commands/${args[1]}.js`];
Object.keys(require.cache).filter((path) => path.includes(`${args[1]}_`)).forEach((path) => delete require.cache[path]);
const cmdIndex = require('.');
let cmd = cmdIndex[args[1]];
if (!cmd) return this.error(message.channel, 'Could not find file.');
cmd = require(`${corepath}/commands/${args[1]}`).default;
this.client.commands.remove(args[1]);
this.client.loadCommand(cmd);
delete require.cache[`${corepath}/commands/index.js`];
delete require.cache[`${corepath}/commands/${args[1]}.js`];
Object.keys(require.cache).filter((path) => path.includes(`${args[1]}_`)).forEach((path) => delete require.cache[path]);
} catch (error) {
if (error.message.includes('Cannot find module')) return this.error(message.channel, 'Could not find file.');
throw error;
}
}
return this.success(message.channel, `Reloaded ${type}.`);
} catch (error) {
return this.client.util.handleError(error, message, this);
}
}
}