cloudservices/src/commands/load.ts

51 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-11-30 18:47:47 -05:00
import { Message } from 'eris';
import { Client } from '..';
import { Command } from '../class';
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'];
2019-11-30 18:53:23 -05:00
this.permissions = { users: ['253600545972027394', '278620217221971968'] };
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]);
2019-12-16 20:27:12 -05:00
const allowed = ['config', 'util', 'command'];
2019-11-30 18:47:47 -05:00
const type = args[0].toLowerCase();
2019-12-16 20:46:17 -05:00
if (!allowed.includes(type)) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Invalid type to load***`);
2019-11-30 18:47:47 -05:00
const corepath = '/var/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-11-30 20:29:24 -05:00
const cmdIndex = require('../commands');
2019-11-30 19:22:51 -05:00
let Cmd = cmdIndex[args[1]];
2019-11-30 18:59:13 -05:00
if (!Cmd) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Could not find file***`);
2019-11-30 19:38:01 -05:00
Cmd = require(`${corepath}/commands/${args[1]}`).default;
2019-11-30 18:47:47 -05:00
this.client.commands.remove(args[1]);
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-11-30 18:47:47 -05:00
} catch (error) {
if (error.message.includes('Cannot find module')) return message.channel.createMessage(`${this.client.stores.emojis} ***Cannot find file***`);
throw error;
}
}
return message.channel.createMessage(`${this.client.stores.emojis.success} Reloaded ${type}`);
} catch (error) {
return this.client.util.handleError(error, message, this);
}
}
}