- Discord bot error

When I run node . I keep getting the error “ReferenceError: Cannot access ‘client’ before initialization”
my script is…

const { Client, Intents } = require(“discord.js”)

const client = new Client({

Intents:[

    Intents.FLAGS.GUILDS,

    Intents.FLAGS.GUILD_MEMBERS,

    Intents.FLAGS.GUILD_MESSAGES

]

})

const config = require(“./config.js”)

client.login(config.TOKEN)

client.on(“ready”, async () => {

console.log(“Im Login!”)

client.user.setactivity(“Hello World!”)

})

Alright, so I went ahead and updated your code to the latest version of discord.js (v14).

There were a few things I had to change or swap-out, I will show you the old code that you had, and will provide the new code that you need to use.

If you’re using a different version of discord, Its not a problem but I do recommend updating to v14.

Old Code

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

const client = new Client({

Intents:[

    Intents.FLAGS.GUILDS,

    Intents.FLAGS.GUILD_MEMBERS,

    Intents.FLAGS.GUILD_MESSAGES

]
})

const config = require("./config.js")

client.login(config.TOKEN)

client.on("ready", async () => {

console.log("Im Login!")

client.user.setactivity("Hello World!")

})

Updated Code

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

client.on("ready", async () => {
  client.user.setActivity("Hello World!");
  console.log("Im Login!")
});

client.login(config.token);

If you have any questions, or any issues with the updated code, please don’t hesitate to let me know!

Happy Coding,
Roman Beard

1 Like

did you find out what line was causing that specific error in the original? it looked like client gets initialized pretty early

Hi! Please don’t bump old posts :grinning: