【问题标题】:Why doesn't my discord bot get online when I type node main.js?为什么当我输入 node main.js 时我的不和谐机器人无法上线?
【发布时间】:2021-11-29 14:55:32
【问题描述】:

所以我首先安装了 node.js,然后在命令提示符下键入 npm init,然后安装了 discord.js(安装 discord.js 时,它没有在项目中添加 node_modules 文件夹,我认为这就是问题的根源)。但是当我尝试让我的机器人在线(使用 node main.js 命令)时,我看到了这个错误消息: 错误:

Cannot find module 'discord.js'
Require stack:
- C:\Users\Cookie\Desktop\bot\main.js
←[90m    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:778:27)←[39m
←[90m    at Module.require (node:internal/modules/cjs/loader:1005:19)←[39m
←[90m    at require (node:internal/modules/cjs/helpers:102:18)←[39m
    at Object.<anonymous> (C:\Users\Cookie\Desktop\bot\main.js:1:17)
←[90m    at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: [ ←[32m'C:\\Users\\Cookie\\Desktop\\bot\\main.js'←[39m ]
}

这是我的 package.json 代码:

{
  "name": "bot",
  "version": "1.0.0",
  "description": "test",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Cookie482",
  "license": "ISC",
  "dependencies": {
    "discord.js": "*"
  }

和 main.js:

const Discord = require('discord.js');

const client = new Discord.Client();

client.once('ready', () => {
    console.log('bot is online');
});

client.login('bot-token');

【问题讨论】:

  • 你的依赖中应该有一个版本作为discord js的key,重新安装试试
  • 该依赖项是如何出现在 package.json 中的?在您执行npm install packagename 之后,依赖项通常不是这样的。你是手写的吗?删除该行并执行npm install discord.js

标签: javascript json discord discord.js


【解决方案1】:

正如 derpirscher 所说,从 package.json 中删除行 "discord.js": "*" 并运行 npm install discord.js

您的main.js 代码已过时。从 v13 开始,您需要指定意图。将const client = new Discord.Client(); 替换为

const client = new Discord.Client({
    intents: [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES
    ]
});

或文档here中指定的其他一些意图

【讨论】:

    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2021-08-03
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多