【问题标题】:My deploycommands.js file is not running when I use a script and throwing an error when I run it manually我的 deploycommands.js 文件在我使用脚本时没有运行,当我手动运行它时抛出错误
【发布时间】:2022-01-13 23:48:03
【问题描述】:

在我的 discord 机器人中,我试图在启动脚本中运行 2 个文件:index.jsdeploycommands.js。它正在正确运行index.js,但没有运行deploycommands.js

当我使用 node deploycommands.js 手动运行 deploycommands.js 时,它会引发错误,我在下面提供了该错误。

deploycommands.js(文件在根目录下)

//Imports
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');

const rest = new REST({ version: '9' }).setToken(process.env.token);

const commands = [];

const guilds = [
  '833289549341523998',
    '804731876950147122',
    '793019059691192370',
    '853617621547352104',
    '872713876284792892',
  '882096964295082004',
  '851070792196030524',
  '904751148404203522',
]


//Pushing command files to 'commands' array
fs.readdirSync("./commands").forEach(dir => {
  fs.readdir(`./commands/${dir}`, (err, files) => {
    if (err) {
      throw err
      return
    }

    const commandFiles = files.filter(file => file.endsWith(".js"));

    if (commandFiles.length <= 0) return

    commandFiles.forEach(file => {
      const command = require(`./commands/${dir}/${file}`);
      try {
        commands.push(command)
      } catch (err) {
        return console.log(`[ERROR] - ${error}`);
      }
    });
  });
});


//Pushing the array to guild commands
for (const guild of guilds) {

  (async () => {
      try {
          console.log('Started refreshing guild commands.');

          await rest.put(
              Routes.applicationGuildCommands(process.env.clientId, guild),
              { body: commands },
          );

          console.log('Successfully reloaded guild commands.');
      } catch (error) {
          console.error(error);
      }
  })();
}

//Pushing the array to global commands
(async () => {
      try {
          console.log('Started refreshing global commands.');

          await rest.put(
              Routes.applicationCommands(process.env.clientId),
              { body: commands },
          );

          console.log('Successfully reloaded global commands.');
      } catch (error) {
          console.error(error);
      }
  })();

我用来运行机器人的脚本:

{
    "scripts": {
        "index": "node .",
        "deploycommands": "node deploycommands.js",
        "start": "npm run index && npm run deploycommands",
}
}

我正在使用npm start 运行机器人

当我尝试手动运行deploycommands.js 时抛出的错误

/home/runner/MultiBot/node_modules/@discordjs/rest/dist/lib/RequestManager.js:66
        const hash = this.hashes.get(`${request.method}:${routeID.bucketRoute}`) ?? `Global(${request.method}:${routeID.bucketRoute})`;
                                                                                  ^

SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/runner/MultiBot/node_modules/@discordjs/rest/dist/index.js:7:22)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

编辑:

错误是由deploycommands.js中的第一次导入引起的:const { REST } = require('@discordjs/rest');。 当我注释掉该行时,它会在第 6 行引发错误,提示 REST is not defined,这是预期的

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

您可以在安装包时尝试--save-exact 标志来强制节点包管理器存储确切的模块版本。

npm install node@16.13.1 --save-dev --save-exact

【讨论】:

  • 能够将节点保留在我想要的版本中,但不幸的是,将节点保留在该版本中并没有解决我的问题。但是感谢您的帮助! :)
【解决方案2】:

从我的实验中,我发现当您尝试更新节点版本时,只有 replit 中的 shell 会被更新。这就是为什么它在Console 选项卡上显示 v12 并因此出现此错误的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 2016-01-13
    • 2012-05-30
    相关资源
    最近更新 更多