简体   繁体   中英

Discord.js reads Intents as undefined

My simple test code

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const { MessageEmbed, channels } = require('discord.js');

client.login('no peeking');

client.on('ready', readyUser);

function readyUser(){
    console.log('Bot is ready and logged in');
}

client.on("messageCreate", (message) => {
    const prefix = "!";
    const args = message.content.substring(prefix.length).split(" ");
    const command = args.shift().toLowerCase();
    if (message.author.id != client.user.id) {
    }
});

gives error

TypeError: Cannot read properties of undefined (reading 'FLAGS') regarding line 2.

From what I found, this could be caused by my intents being disabled in Discord developer portal. Even though I enabled them and the problem is still in place.

What I found (enabled intents)

In discord.js v14, you need to use GatewayIntentBits when declaring the intents in the client. An example would be something like this:

const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        // ...
    ]
})

If you're using v14 of discord.js, Intents are no longer available.

You'll need to use GatewayIntentBits instead:

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

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