From dffe4f5aeec4091f9361bd1403bae4264007db42 Mon Sep 17 00:00:00 2001 From: rhearmas <34490428+qu-ota@users.noreply.github.com> Date: Fri, 20 Dec 2019 20:48:18 -0500 Subject: [PATCH] add unloadCommand --- modules/functions.js | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/modules/functions.js b/modules/functions.js index 163723d..a02982d 100644 --- a/modules/functions.js +++ b/modules/functions.js @@ -76,6 +76,68 @@ module.exports = (client) => { return `Unable to load command ${commandName}: ${e}`; } }; + + client.unloadCommand = async (commandName) => { + let command; + if (client.commands.has(commandName)) { + command = client.commands.get(commandName); + } else if (client.aliases.has(commandName)) { + command = client.commands.get(client.aliases.get(commandName)); + } + if (!command) return `The command \`${commandName}\` doesn\'t seem to exist, nor is it an alias. Try again!`; + + if (command.shutdown) { + await command.shutdown(client); + } + const mod = require.cache[require.resolve(`../commands/${command.help.category}/${command.help.name}`)]; + delete require.cache[require.resolve(`../commands/${command.help.category}/${command.help.name}.js`)]; + for (let i = 0; i < mod.parent.children.length; i++) { + if (mod.parent.children[i] === mod) { + mod.parent.children.splice(i, 1); + break; + } + } + return false; + }; + + client.reloadCommand = async(commandName) => { + // step 1: unload the command + let command; + if (client.commands.has(commandName)) { + command = client.commands.get(commandName); + } else if (client.aliases.has(commandName)) { + command = client.commands.get(client.aliases.get(commandName)); + } + if (!command) return `The command \`${commandName}\` doesn\'t seem to exist, nor is it an alias. Try again!`; + + if (command.shutdown) { + await command.shutdown(client); + } + const mod = require.cache[require.resolve(`../commands/${command.help.category}/${commandName}.js`)]; + delete require.cache[require.resolve(`../commands/${command.help.category}/${commandName}.js`)]; + for (let i = 0; i < mod.parent.children.length; i++) { + if (mod.parent.children[i] === mod) { + mod.parent.children.splice(i, 1); + break; + } + } + return false; + + // step 2: load said command + try { + const props = require(`../commands/${command.help.category}/${command.Name}`); + if (props.init) { + props.init(client); + } + client.commands.set(props.help.name, props); + props.conf.aliases.forEach(alias => { + client.aliases.set(alias, props.help.name); + }); + return false; + } catch (e) { + return `Unable to load command ${commandName}: ${e}`; + } + } Object.defineProperty(String.prototype, "toProperCase", { value: function() {