Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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")
like image 232
Raul Unc Avatar asked May 30 '26 09:05

Raul Unc


1 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);
});
like image 158
Syntle Avatar answered Jun 01 '26 23:06

Syntle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!