因此,我首先安装了node.js,然后在命令提示符下输入npm init,然后安装了discord.js (在安装discord.js时,它没有在项目中添加node_modules文件夹,我认为这就是问题的根源)。但是当我尝试让我的机器人上线时(使用node main.js命令),我看到这个错误消息: Error:
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');发布于 2021-10-11 00:03:10
正如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中指定的一些其他意图。
https://stackoverflow.com/questions/69516338
复制相似问题