简体   繁体   中英

check if an user is in a specific guild discord.js

I don't know if it's possible, but I'm trying to make my bot returning all the servers it shares with an user .

So I got all the ids from the guilds' where the bot is, and the id from the user.

And I don't know how to check for each guild if the user's id is on it or not.

I tried to do this:

let user_id = message.author.id;

let guild_list = [];

client.guilds.cache.forEach(guild => {
    guild_list.push(guild.id);
    })

for (let i = 0; i < guild_list.length; i++){

    let thisGuild = client.guilds.cache.get(guild_list[i]);

    if(thisGuild.member(user_id)){

        message.channel.send("Shared server : " + guild_list[i]);

    }

}

But the Guildmember class only checks the user's id on the server where the command is run. I don't know if it's possible, and I don't find anything that could help me in the documentation.

It is very much possible...

We can use the Collector#filter method for this since we would want to filter the guild(s) the bot and the author is.

const mutualGuilds = client.guilds.cache.filter((guild) => {
   return guild.members.cache.has(message.author.id);
});

    // This will log the mutual guild(s) the bot and the author is in, of course you can change it however you'd like.
console.log(mutualGuilds);

It is as easy as this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM