【问题标题】:How to load new commands without restarting bot如何在不重新启动机器人的情况下加载新命令
【发布时间】:2020-07-24 02:48:43
【问题描述】:

我有一个工作机器人,有一个正常工作的命令处理程序,我有一个重新加载命令来更新我的代码以获取预先存在的命令。每当我添加一个新命令时,我都必须重新启动整个机器人。由于这个特定的机器人有运行间隔的脚本,重新启动我的机器人将终止所有运行间隔,迫使所有用户手动重新启动它们。我不想每次添加新命令时都不得不重启我的机器人,所以我需要帮助。

这是我现在的重新加载命令:

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}`");
        }
    },
};

我想在命令名称前添加一个可选的“新”参数来专门查找新命令,因为当前代码有效,但它只看到预先存在的命令。如果更改当前代码以另外查找新命令会更简单,但如果没有找到仍然会出错,那也很好。

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    是的,您可以使用以下代码,如果该命令已加载,则会将其删除。

    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}`");
            }
        },
    };
    

    【讨论】:

    • 在使用 --new 时如何添加对现有命令的检查?以防万一我不小心将 --new 参数与预先存在的命令一起使用,那么我将有一个可能导致问题的重复要求...
    • 我注意到您修改了 if 语句,但您使用了“&&”。你能解释一下你为什么这样做吗?在我看来,如果我使用 --new 参数,那么 if 语句无论如何都会返回 false,并跳过删除。使用“||”不是更好吗如果两者都返回false,那只会跳过删除?
    • 我更新了代码,这样它就可以在不使用 --new 的情况下工作,而且更简单
    • 你会如何为 python 做到这一点(我的意思是重新加载而不重新启动)?
    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 2012-05-13
    • 2014-03-11
    • 1970-01-01
    • 2011-07-10
    • 2012-10-03
    相关资源
    最近更新 更多