Add support for reloading modules(commands) without restarting the bot.
This commit is contained in:
parent
35d47e4a11
commit
acd2e2a502
3 changed files with 61 additions and 9 deletions
31
commands/reload.js
Normal file
31
commands/reload.js
Normal file
|
@ -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)
|
||||
}
|
||||
};
|
12
server.js
12
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-------`)
|
||||
}
|
||||
});
|
||||
|
|
27
util/reloadCommands.js
Normal file
27
util/reloadCommands.js
Normal file
|
@ -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')])
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue