TypeError: Cannot read properties of undefined (reading 'FLAGS') when trying to do 'node index.js'

Viewed 406

I am trying to make a discord bot using the OPENAI API and when I try to use the command 'node index' I get the following error:

[ERROR IMAGE][1]

index.js FILE:

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

const bot = new Client({
    intents: [Intents.FLAGS.Guild, Intents.FLAGS.Guild_Members, Intents.FLAGS.Guild_Messages]
});

bot.on('ready', () => {
    console.log(`Bot ${bot.user.tag} is logged in!`);
});

bot.login('OTk5MTQ0NjU3ODgxNjc3ODY1.GvMgQX.dz3r43fDHi6nqB_cO55gSHhO2jJJ-zBKQIjF5k');

bot.on('ready', () => {
    console.log(`Bot ${bot.user.tag} is logged in!`);
  });

Package.json file:

{
   "name": "discord-gpt3-bot",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
},
      "repository": {
        "type": "git",
        "url": "git+https://github.com/nrrpatel/discordbot.git"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "bugs": {
        "url": "https://github.com/nrrpatel/discordbot/issues"
      },
      "homepage": "https://github.com/nrrpatel/discordbot#readme",
      "dependencies": {
        "discord.js": "^14.0.3",
        "dotenv": "^16.0.1",
        "openai-api": "^1.3.1",
        "typescript": "^4.7.4"
      }
    }
1 Answers

It looks like you're using an older implementation for the newest version of discord.js. If you follow the changelog for its v14, you'll see the client initialization changed.

So instead of:

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

You should be using:

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