diff --git a/commands/reload.js b/commands/reload.js new file mode 100644 index 0000000..339d434 --- /dev/null +++ b/commands/reload.js @@ -0,0 +1,31 @@ +const Discord = require('discord.js'); + +module.exports = { + name: 'reload', + description: 'Reloads modules.', + admin: true, + execute(message, client) { + + let reloadCommands = require("../util/reloadCommands.js") + let beforeSize = client.commands.size; + let sendText; + + reloadCommands(client) + if (beforeSize > client.commands.size) { + let difference = beforeSize - client.commands.size; + if (difference == 1) + sendText = `${difference} module was removed and ${client.commands.size} in total were reloaded.` + else + sendText = `${difference} modules were removed and ${client.commands.size} in total were reloaded.` + } else if (beforeSize < client.commands.size) { + let difference = client.commands.size - beforeSize; + if (difference == 1) + sendText = `${difference} module was added and ${client.commands.size} in total were reloaded` + else + sendText = `${difference} modules were added and ${client.commands.size} in total were reloaded` + } else if (beforeSize === client.commands.size) { + sendText = `${client.commands.size} modules were reloaded` + } + message.channel.send(sendText) + } +}; \ No newline at end of file diff --git a/server.js b/server.js index dae894d..1612b21 100644 --- a/server.js +++ b/server.js @@ -8,14 +8,8 @@ const { client.commands = new Discord.Collection(); -const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); - -for (const file of commandFiles) { - const command = require(`./commands/${file}`); - client.commands.set(command.name, command); -} - -console.log(client.commands); +var reloadCommands = require("./util/reloadCommands.js") +reloadCommands(client) client.once('ready', () => { console.log('Ready!'); @@ -44,6 +38,7 @@ client.on('message', async message => { switch (commandName) { case "ban": case "botinfo": + case "reload": command.execute(message, client, args); break; case "say": @@ -57,7 +52,6 @@ client.on('message', async message => { command.execute(message) } } catch (error) { - message.channel.send("That command either does not exist, or is broken.") console.log(`${error}\n-------`) } }); diff --git a/util/reloadCommands.js b/util/reloadCommands.js new file mode 100644 index 0000000..520c3f4 --- /dev/null +++ b/util/reloadCommands.js @@ -0,0 +1,27 @@ +const fs = require('fs') +const filepath = 'commands/' + + +module.exports = function (client) { //TODO: Add ability to reload specified commands with arguments + + let commandFiles = fs.readdirSync(filepath).filter(file => file.endsWith('.js')); + if (client.commands.size != 0) { + for (const i of commandFiles) { + console.log(i) + delete require.cache[require.resolve(`../${filepath}${i}`)]; + } + } + + console.log(client.commands.size) + client.commands.clear() + console.log("test") + + for (const file of commandFiles) { + const command = require(`../commands/${file}`); + client.commands.set(command.name, command); + } + + + //console.log(require.cache[require.resolve('../commands')]) +} +