繁体   English   中英

无法部署我的斜杠命令 Discord.js

[英]Can't deploy my slash commands Discord.js

  • 编辑:原来我还在节点 16.4.4 上,这就是问题所在,斜杠命令需要节点 16.6 或更高版本。 发生这种情况是因为我切换了机器并将我的 node_modules 文件夹添加到我正在使用的 git 中。

运行我的guild.commands.jsdeploy-commands.js会产生此错误:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '../common'
Require stack:
- C:\Users\Thomas\Documents\discordBot\node_modules\discord-api-types\payloads\v9\index.js
- C:\Users\Thomas\Documents\discordBot\node_modules\discord-api-types\v9.js
- C:\Users\Thomas\Documents\discordBot\deploy-commands.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\Thomas\\Documents\\discordBot\\node_modules\\discord-api-types\\payloads\\v9\\index.js',
    'C:\\Users\\Thomas\\Documents\\discordBot\\node_modules\\discord-api-types\\v9.js',
  ]
}

我已将 scope 设置为application.commandsbot ,并且我已授予 bot 所有权限。 我的第一个斜杠命令运行正常,但我无法添加新命令。 我已经尝试过节点 16.4 和 17.4,并尝试重新安装从节点到 discord.js 的所有内容。公共模块总是出现相同的错误。 我正在关注本教程: https://discordjs.guide/creating-your-bot/command-handling.html#individual-command-files

这是deploy-commands.js

const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');
const rest = new REST({ version: '9' }).setToken(token);

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
}

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(() => console.log('Successfully registered application commands.'))
    .catch(console.error);

我的部署脚本几乎相同并且工作正常。 从错误消息Cannot find module '../common'来看,它看起来像是一个 require 错误。

我在您的代码示例中没有看到常见的要求。 尝试找到 require 并使用诸如'./common''common'之类的变体检查你的路径(如果你不知道它在哪里,你可以使用 Ctrl + Maj + F 运行项目搜索)

它可以是未经重构而移动的文件或文件夹。

我希望这有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM