Skip to content Skip to sidebar Skip to footer

Listing User Ids From All Guilds (discord.js)

I'm aware that some of the elements in this code has probably been deprecated. However, I found this code and I have altered it to some extent but I am receiving a error. I'm looki

Solution 1:

Firstly, you won’t get ratelimited for doing things that are only on your side. Secondly, how you do this is by fetching client.users.

//async functionconst users = await client.users.fetch()
//users is a Collection of user objects. You can map by user idconst userIds = users.map(u => u.id)
//userIds is an Array of user Ids

Solution 2:

You don't need to fetch the users, they are already cached. You can't use GuildManager.fetch() nor UserManager.fetch() to fetch all the users or guilds, since it has an id (Twitter Snowflake) as a required parameter (referring to @MrMythical's answer).

When a discord bot is started an event GUILD_CREATE is emitted on the websocket for each guild. The client uses this to cache all the .guilds and .users, etc.

So you can simply use:

// An array of user idsconst users = client.users.cache.map(u => u.id);

To implement this in a command:

client.on("message", (message) => {

    if (message.author.id == client.user.id) return;

    if (message.content == "$listmids") {
        const users = client.users.cache.map(u => u.id);
        console.log(`Listing user ids from all guilds:`);
        console.log(users);
    }

});

enter image description here

You can also use the websocket event GUILD_CREATE to log all user ids to console on bot's startup:

client.ws.on("GUILD_CREATE", (data) => {
    console.log(`Listing user ids from a guild '${data.name}'`);
    console.log(data.members.map(m => m.user.id));
});

enter image description here


As for compatibility with discord.js 13.x.x, it is fully compatible, but you need GUILDS intent to receive GUILD_CREATE on the websocket. And GUILD_PRESENCES intent to include the users in the guild data.

Also GUILD_MESSAGES for the command to work. And listen (client.on(...)) for messageCreate instead of message, it is deprecated.

const client = newDiscord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES"] });

Post a Comment for "Listing User Ids From All Guilds (discord.js)"