簡體   English   中英

如何從 discord.js 正確導入 Intent?

[英]How can I properly import Intents from discord.js?

我在從 discord.js 導入時遇到問題。

import { Client, Intents } from 'discord.js'

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

const token = 'mytoken'
client.on('ready', () => {
    console.log('bot logged in')
})

client.login(token)

當我運行 npm index.js 我得到這個錯誤: Intent error

我安裝的 discord.js package 是否已損壞或者可能沒有完全安裝/正確安裝? 這是我第一次搞亂 discord api 所以我還不太熟悉。 閱讀文檔,但仍然對為什么我的無法正確導入感到困惑。

我認為您首先錯過了導入 Discord 。 你快到了。 試試這個,

import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Flags.Guilds, GatewayIntentBits.Flags.GuildMessages] });
const token = 'mytoken'
client.on('ready', () => {
    console.log('bot logged in')
})

client.login(token)

顯然在 Discord.js Intents 的 v14 中更改為 GatewayIntentBits: https://discordjs.guide/additional-info/changes-in-v14.html#enum-values

// import Discord from 'discord.js'
import { Client, GatewayIntentBits } from 'discord.js'
// const Discord = require("discord.js");
// const { Client, GatewayIntentBits } = require("discord.js")

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

const token = 'mytoken'
client.on('ready', () => {
    console.log('bot logged in')
})
client.login(token)

暫無
暫無

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

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