簡體   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