ShadowGamer3
ShadowGamer3

Reputation: 49

How to load new commands without restarting bot

I have a working bot, with a functioning command handler, and I have a reload command to update my code for preexisting commands. Anytime I add a new command, I have to restart the whole bot. Since this particular bot has scripts on intervals running, restarting my bot would terminate all running intervals, forcing all users to manually restart them. I don't want to have to resort to restarting my bot anytime I add a new command, so I need help.

Here's my reload command right now:

const botconfig = require("../config.json");

module.exports = {
    name: 'reload',
    type: "Developer",
    description: 'Reloads a command (developer only)',
    cooldown: 1,
    execute(message, args) {

        if (message.author.id != botconfig.developerid) return message.channel.send("Only my developer can use this command...");
        message.channel.send("Developer command confirmed!");

        if (!args.length) return message.channel.send(`You didn't pass any command to reload!`);
        const commandName = args[0].toLowerCase();
        const command = message.client.commands.get(commandName) ||
            message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

        if (!command) return message.channel.send(`There is no command with name or alias \`${commandName}\`, ${message.author}!`);

        delete require.cache[require.resolve(`./${command.name}.js`)];

        try {
            const newCommand = require(`./${command.name}.js`);
            message.client.commands.set(newCommand.name, newCommand);
            message.channel.send("Command `" + command.name + "` was reloaded!");
        } catch (error) {
            console.log(error);
            message.channel.send("There was an error while reloading the `" + botconfig.prefix + command.name + "` command. \n\nError is as follows:\n``${error.message}`");
        }
    },
};

I want to add an optional "new" argument before the command name to look specifically for new commands, as the current code works, but it only sees preexisting commands. If it would be simpler to change the current code to look for new commands in addition, but still error out if it finds none, that'll be fine too.

Upvotes: 0

Views: 3771

Answers (1)

Androz2091
Androz2091

Reputation: 3005

Yes, you can use the following code so if the command is already loaded, it deletes it.

const botconfig = require("../config.json");

module.exports = {
    name: 'reload',
    type: "Developer",
    description: 'Reloads a command (developer only)',
    cooldown: 1,
    execute(message, args) {

        if (message.author.id != botconfig.developerid) return message.channel.send("Only my developer can use this command...");
        message.channel.send("Developer command confirmed!");

        if (!args.length) return message.channel.send(`You didn't pass any command to reload!`);
        const commandName = args[0].toLowerCase();

        if(message.client.commands.get(commandName)){
            const command = message.client.commands.get(commandName) ||
            message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

            if (!command) return message.channel.send(`There is no command with name or alias \`${commandName}\`, ${message.author}!`);

            delete require.cache[require.resolve(`./${command.name}.js`)];
        }

        try {
            const newCommand = require(`./${commandName}.js`);
            message.client.commands.set(commandName, newCommand);
            message.channel.send("Command `" + commandName+ "` was reloaded!");
        } catch (error) {
            console.log(error);
            message.channel.send("There was an error while reloading the `" + botconfig.prefix + commandName + "` command. \n\nError is as follows:\n``${error.message}`");
        }
    },
};

Upvotes: 1

Related Questions