Search code examples
javascriptnode.jsdiscorddiscord.js

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)


Solution

  • 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,
            // ...
        ]
    })