簡體   English   中英

Discord.js:無效的位域標志或編號:GUILDS

[英]Discord.js: Invalid bitfield flag or number: GUILDS

我有我的第一個 go 來制作 Discord 機器人。 代碼非常基本,只是一個在啟動時將自己的標簽記錄到控制台的機器人:

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

const TOKEN = "REDACTED"

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

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}`)
})

client.login(TOKEN)

但是,在 VSCode 終端中鍵入node index.js后,我收到以下錯誤:

PS C:\Users\15055\Documents\Alt Formatter> node index.js
C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:168
    throw new RangeError(ErrorCodes.BitFieldInvalid, bit);
    ^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
    at IntentsBitField.resolve (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:168:11)
    at C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:163:54
    at Array.map (<anonymous>)
    at IntentsBitField.resolve (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\util\BitField.js:163:40)
    at Client._validateOptions (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\client\Client.js:481:41)
    at new Client (C:\Users\15055\Documents\Alt Formatter\node_modules\discord.js\src\client\Client.js:78:10)
    at Object.<anonymous> (C:\Users\15055\Documents\Alt Formatter\index.js:9:16)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32) {
  [Symbol(code)]: 11
}

Node.js v18.4.0

我已經使用命令npm i discord.js安裝了 discord.js,所以我使用的是 v14。

根據文檔,您不能使用字符串作為意圖名稱,但您可以從包中導入它們,因此在您的情況下類似於

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

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

應該做的伎倆。

在 discord.js v14 中,可以從GatewayIntentBits獲得意圖標志。

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Discord.Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
  ]
})

變更清單:

v12/v13 v14
GUILDS GatewayIntentBits.Guilds
GUILD_BANS GatewayIntentBits.GuildBans
GUILD_EMOJIS_AND_STICKERS GatewayIntentBits.GuildEmojisAndStickers
GUILD_INTEGRATIONS GatewayIntentBits.GuildIntegrations
GUILD_INVITES GatewayIntentBits.GuildInvites
GUILD_MEMBERS GatewayIntentBits.GuildMembers
GUILD_MESSAGE_REACTIONS GatewayIntentBits.GuildMessageReactions
GUILD_MESSAGE_TYPING GatewayIntentBits.GuildMessageTyping
GUILD_MESSAGES GatewayIntentBits.GuildMessages
GUILD_PRESENCES GatewayIntentBits.GuildPresences
GUILD_SCHEDULED_EVENTS GatewayIntentBits.GuildScheduledEvents
GUILD_VOICE_STATES GatewayIntentBits.GuildVoiceStates
GUILD_WEBHOOKS GatewayIntentBits.GuildWebhooks
DIRECT_MESSAGES GatewayIntentBits.DirectMessages
DIRECT_MESSAGE_TYPING GatewayIntentBits.DirectMessageTyping
DIRECT_MESSAGE_REACTIONS GatewayIntentBits.DirectMessageReactions

我根據文檔找到的解決方案是:

const { Client, GatewayIntentBits } = require("discord.js");

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
    ]});

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM