简体   繁体   中英

How do I find all members with a specific role on 'ready'? Discord.js

I'm aware that this question has been asked before, but I'm yet to find a solid answer for the newest version of Discord.JS. I'd like to find all of the members in the guild hostGuild that have the role 'xyz'.

client.on('ready', () => {
    const hostGuild = client.guilds.cache.get('822234110369595443');
    //get users with role 'xyz'
    }
});

Have you tried .members.cache ? It would look like this:

const membersWithRole = hostGuild.members.cache.filter(m => m.roles.cache.has('roleid'));

EDIT: It doesn't seem to work, because the members and roles are not cached. You will need to use async/await here:

const allMembers = await hostGuild.members.fetch();
const membersWithRole = allMembers.filter(m => m.roles.cache.has('roleid'));

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