Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list the all Discord servers ID where my bot are in the console? | discord.js

I tried on googling but all of the errors was "is not a function", that's because i'm using discord.js V12? I can't find it on google, there is less question about this one. This was my code.

var serverArray = client.guilds.array();
for(i = 0; i < serverArray.length; i++) {
    console.log("Server ID: " + serverArray[i].id);
}
like image 607
randomname123 Avatar asked Oct 22 '25 07:10

randomname123


2 Answers

client.guilds.cache.forEach(guild => {
  console.log(`${guild.name} | ${guild.id}`);
})
like image 80
rez Avatar answered Oct 23 '25 19:10

rez


let clientguilds = client.guilds.cache()
console.log(clientguilds.map(g => g.id) || "None")

This should do the trick! It's going to cache all the guilds your bot is in and then it will map the guilds as an array. We then get the id of each guild or, if it's not in any guilds "none".

like image 28
Washi Avatar answered Oct 23 '25 19:10

Washi



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!