有没有办法在不终止进程的情况下更新我的不一致机器人的代码?

例如,如果我更改了命令代码中的单个字母,我是否必须完全重新启动机器人才能更新它?那么非命令代码呢?

我最近重启了我的机器人,以更新/测试代码,Discorde给我发了一条消息,说我的机器人重启太频繁了.

我应该忽略该消息,或者您有无需重新启动即可更新的解决方案吗?

谢谢.

推荐答案

例如,如果命令处理程序是这样创建的,

index.js

const { Collection } = require("discord.js");

client.slash = new Collection();
const slashCommandFolders = fs.readdirSync("./slash");
for (const folder of slashCommandFolders) {
  const commandFiles = fs
    .readdirSync(`./slash/${folder}`)
    .filter((file) => file.endsWith(".js"));
  for (const file of commandFiles) {
    const command = require(`./slash/${folder}/${file}`);
    client.slash.set(command.name, command);
  }
}

(命令文件在slash/<category>/<command_name>.js中)

slash/general/ping.js

const { CommandInteraction } = require("discord.js");

module.exports = {
  name: "ping",
  /**
   *
   * @param {CommandInteraction} i
   */
  execute(i) {
    i.reply({ content: "Pong" });
  },
};

(interactionCreate事件需要正确处理,但我们在这里将跳过这一点)

通过创建这样的重新加载命令:

src/admin/reload.js

const {
  CommandInteraction,
  Client,
  CommandInteractionOptionResolver,
} = require("discord.js");
const fs = require("fs");

module.exports = {
  name: "reload",
  /**
   *
   * @param {CommandInteraction} i
   * @param {Client} client
   * @param {CommandInteractionOptionResolver} options
   */
  async execute(i, client, options) {
    const commandName = options.getString("command").toLowerCase();
    const command =
      i.client.slash.get(commandName) ||
      i.client.slash.find(
        (cmd) => cmd.aliases && cmd.aliases.includes(commandName)
      );

    if (!command)
      return i.reply({ content: "No command found", ephemeral: true });
    const commandFolders = fs.readdirSync("./slash");
    const folderName = commandFolders.find((folder) =>
      fs.readdirSync(`./slash/${folder}`).includes(`${command.name}.js`)
    );
    delete require.cache[
      require.resolve(`../${folderName}/${command.name}.js`)
    ];

    try {
      const newCommand = require(`../${folderName}/${command.name}.js`);
      i.client.slash.set(newCommand.name, newCommand);
      i.reply({ content: "reloaded", ephemeral: true });
    } catch (error) {
      console.error(error);
      i.reply({ content: "reload failed", ephemeral: true });
    }
  },
};

我们可以应用更改,而无需重启bot.所以衣冠楚楚的系统

Javascript相关问答推荐

为什么子组件没有在reaction中渲染?

如何使用Echart 5.5.0创建箱形图

深嵌套的ng-container元素仍然可以在Angular 布局组件中正确渲染内容吗?

模块与独立组件

嵌套异步JavaScript(微任务和macrotask队列)

如何使覆盖div与可水平滚动的父div相关?

在Vite React库中添加子模块路径

你怎么看啦啦队的回应?

如何在使用rhandsontable生成表时扩展数字输入验证?

如何创建返回不带`new`关键字的实例的类

当用户点击保存按钮时,如何实现任务的更改?

如何在 Select 文本时停止Click事件?

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

JS Animate()方法未按预期工作

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

无法设置RazorPay订阅API项目价格

Node.js错误: node 不可单击或不是元素

我正在试着做一个TicTacToe Ai来和我玩.但是,我试着在第一个方块被点击时出现一个X,然后在第二个方块之后出现一个O

使用CEPRESS截取时,cy.Wait()在等待5000ms的第一个路由请求时超时

为什么NULL不能在构造函数的.Prototype中工作