简体   繁体   中英

How can I properly import Intents from discord.js?

I am having an issue getting my intents to import from 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)

When I run npm index.js I get this error: Intent error

Could my installation of the discord.js package be corrupt or maybe didnt install all the way/properly? This is my first time messing with the discord api so im not too familiar with it all yet. Reading documentation and still confused as to why mine wont import properly.

I think you missed to import Discord on the first place. You are almost there. Just try this out,

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)

Apparently in v14 of Discord.js Intents was changed to 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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