如何在不重启机器人的情况下加载新命令

暗影玩家3

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

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

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

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

Androz2091

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不重启的情况下重新加载 .XCompose?

如何在没有给出命令的情况下让我的机器人 DM 特定用户 ID?

如何在不重新启动的情况下更新机器人状态中的“bot.users.size”

如何在不使用“except”python的情况下退出不和谐机器人

如何在不使用 Google API 的情况下制作 Google Classroom 机器人?

如何在不重启的情况下更改RabbitMQ心跳

如何在不将我的机器人托管在Azure中的情况下执行图身份验证?

如何在不重启设备的情况下重启应用系统?

您如何在discord.js中发出重启机器人的命令?

如何让 iOS Teams App 用户在不通知 MicrosoftAppId 的情况下使用我的机器人?

如何在不重启的情况下在 pgpool 中重新加载 pool_hba.conf?

如何在不重启的情况下在 Node-RED 中重新加载节点?

如何在不重启节点的情况下删除Corda节点的数据?

如何在不重启笔记本的情况下清除 jupyter 内存

Linux服务器如何在不重启的情况下更新其内核

如何在不重启的情况下刷新Android Studio中的资源?

如何在不重启Matlab的情况下覆盖mexw32?

如何在不重启的情况下从其他Linux发行版中安装Ubuntu

如何在不重启服务器的情况下终止单个apache连接?

如何在不重启的情况下安全地在systemd上卸载/ var / usr

如何在不重启MySQL的情况下刷新performance_schema统计信息?

如何在不重启的情况下杀死“ fast fork()”进程?

如何在不重启 spring boot 的情况下使用 Spring Security 添加新用户

如何在用户无需问候机器人的情况下启动Twilio自动驾驶仪聊天会话?

如何在没有 turnContext 的情况下从机器人主动触发对话?

如何在不重新加载服务器的情况下重新加载模板?

如何在不重新加载js文件的情况下加载Spring MVC视图

当我想查看更改时,如何在不重启nodejs的情况下编辑服务器文件?

如何在不重启VNC服务器的情况下更改其屏幕分辨率?