admin管理员组

文章数量:1287114

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")

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")
Share Improve this question edited Apr 5, 2020 at 15:33 Danilo Ivanovic 1,2262 gold badges10 silver badges21 bronze badges asked Apr 5, 2020 at 15:18 Raul UncRaul Unc 391 gold badge1 silver badge5 bronze badges 3
  • I believe this question is already answered here. stackoverflow./questions/35969974/… – Danilo Ivanovic Commented Apr 5, 2020 at 15:32
  • What version of Discord.js are you on? – Aci Commented Apr 5, 2020 at 15:48
  • I looked at the answer you linked here but still couldn't solve it – Raul Unc Commented Apr 5, 2020 at 15:49
Add a ment  | 

2 Answers 2

Reset to default 9

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 remend 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

本文标签: javascriptDiscord bot TypeError clientguildsforEach is not a functionStack Overflow