簡體   English   中英

更新所有 bot 命令而不重新加載它(沒有“node .”或 nodemon)

[英]Update ALL bot commands without reloading it (without "node ." or nodemon)

我想更新我的命令代碼,而無需使用 nodemon 或僅使用 node 完全重新啟動機器人node . 我想創建一個命令,我可以發送它來更新我的機器人中的所有命令(基本上是命令集合,它們所在的位置和它們的代碼存儲)。 這是我嘗試使用的代碼:

const { glob } = require('glob')
const { promisify } = require('util')
const globPromise = promisify(glob);

await client.commands.clear()

const commandFiles = await globPromise(`${process.cwd()}/commands/**/*.js`);
commandFiles.map((value) => {
const file = require(value);
const splitted = value.split("/");
const directory = splitted[splitted.length - 2];
    
if (file.name) {
const properties = { directory, ...file };
client.commands.set(file.name, properties);
}
})

結果說我的代碼有效但命令沒有更新,我該如何解決? 以防萬一:我的 djs 版本是 v13.2.0

使用此函數刪除模塊/文件對應的緩存並獲取更新版本:

function requireUncached(module) {
    delete require.cache[require.resolve(module)];
    return require(module);
}

現在,要要求文件,只需將require替換為requireUncached

但是,您可能需要更新每個事件,以防止更新晚

client.on("messageCreate", msg => {
  client.commands.clear();
  const files = fs.readdirSync("./commands") //read commands folder
  const folders = files.filter(f => !f.includes(".")) //get category folders
  for (const folder of folders) {
    const cmdFiles = fs.readdirSync(`./commands/${folder}`).filter(f => f.endsWith(".js")) //get cmd files in each folder
    for (const file of cmdFiles) {
      const f = requireUncached(`./commands/${folder}/${file}`) //require the file "uncached"
      client.commands.set(f.name, f)
    }
  }
})

可能有更有效的方法來做到這一點,確保您不必更新每個事件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM