繁体   English   中英

为什么当我输入 node main.js 时我的 discord bot 没有上线?

[英]Why doesn't my discord bot get online when I type node main.js?

所以我首先安装了 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');

正如 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
    ]
});

此处文档中指定的其他一些意图

快乐编码!

暂无
暂无

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

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