Atom Brecher
Atom Brecher

Reputation: 105

Get all guild ID's with Discord.js

So I want to get every ID from the servers my bot is on. In the best way please in a list so I could get one ID after another because I need them for my prefix system. I tried a lot of designs but it didn't work right.

Upvotes: 6

Views: 28395

Answers (1)

Jakye
Jakye

Reputation: 6625

You can use Collection#map to map Client.guilds.cache (GuildManager#cache) by ID.


Discord JS V12

(not supported anymore)

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

client.on("ready", () => {
    const Guilds = client.guilds.cache.map(guild => guild.id);
    console.log(Guilds);
});

client.login(process.env.DISCORD_ACCESS_TOKEN)

Discord JS V13/14

const Discord = require("discord.js");
const client = new Discord.Client({
    intents: ["GUILDS"],
});

client.on("ready", () => {
    const Guilds = client.guilds.cache.map(guild => guild.id);
    console.log(Guilds);
});

client.login(process.env.DISCORD_ACCESS_TOKEN);

Upvotes: 14

Related Questions