Discord bot TypeError: client.guilds.forEach is not a function

Viewed 10284

I am trying to make my discord bot to print all the servers that it's connected to but when I try to run my code I get this type error: TypeError: client.guilds.forEach is not a function

const Discord = require("discord.js")
const client = new Discord.Client()

client.on("ready", () => {
    console.log("Conectat ca si: " + client.user.tag)

    client.user.setActivity("my brother kiko jr", {type: "WATCHING"})

    client.guilds.forEach((guild) => {
        console.log(guild.name)

    })

})

client.login("xyz")
2 Answers

I am assuming you're getting the error because you're using discord.js v12

If that is the case, your solution would be:

client.guilds.cache.forEach((guild) => {
    console.log(guild.name);
});

no worries the problem ur facing right now is because you may have the version 12 of discord so i'd recommend u to use the following code

client.guilds.cache.forEach((guild)=>{
console.log(guild.name)
})

or try re-installing discord with

npm i discord.js

and ur pc will install the version and u can start using it

Related