'Discord API - Get a list of all the servers a bot is part of

I created a Discord bot that, once invited to a guild, is able to make API calls such as

GET/guilds/{guild.id}/members (https://discord.com/developers/docs/resources/guild#list-guild-members)

But how I do get the guild.id beforehand? Is it possible to make an API call to get a list of all the servers a bot is part of?



Solution 1:[1]

The client you create will have a guilds attribute you can use to access the servers it's in. This will be in the form of a GuildManager object which has a fetch() method to get a collection of guild objects.

Here is an example that gets all of a bot's guilds and prints their ids:

    await bot.guilds.fetch()
    .then(guilds => {
        guilds.forEach(guild => {
            console.log(guild.id);
        });
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 asher26