How can I get all users with a role ID, then scrape their nicknames on the server

Title^
23 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
grass
grass16mo ago
assuming the guild's members are cached, you can use <Role>.members and map those by nickname
nBux
nBuxOP16mo ago
How do I do that
grass
grass16mo ago
you can fetch the members with <Role>.guild.members.fetch(), then use <Role>.members.map()
d.js docs
d.js docs16mo ago
mdn Array.prototype.map() The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
grass
grass16mo ago
you can map by GuildMember.nickname
nBux
nBuxOP16mo ago
What would the code look like?
grass
grass16mo ago
noSpoons explaining how you can
nBux
nBuxOP16mo ago
interaction.guild.roles.fetch('1135435838046621756')
const members = Queued1v1Role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${members}`);
interaction.guild.roles.fetch('1135435838046621756')
const members = Queued1v1Role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${members}`);
grass
grass16mo ago
sure
nBux
nBuxOP16mo ago
const members = Queued1v1Role.members.map(member => member.displayName); that doesnt work tho
grass
grass16mo ago
what do you mean doesnt work
nBux
nBuxOP16mo ago
I get this TypeError: Cannot read properties of undefined (reading 'map')
grass
grass16mo ago
show how you define Queued1v1Role
nBux
nBuxOP16mo ago
const Queued1v1Role = '1135435838046621756';
grass
grass16mo ago
thats just a string you can get the role with <Guild>.roles.cache.get(Quebed1v1Role) no need to fetch, theyre cached if you have the Guilds intent
nBux
nBuxOP16mo ago
if (queueCount1v1>1) {
const guild = interaction.guild;
const role = guild.roles.cache.get(Queued1v1Role);
const membersWithRole = role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${membersWithRole.join(', ')}`);

}
if (queueCount1v1>1) {
const guild = interaction.guild;
const role = guild.roles.cache.get(Queued1v1Role);
const membersWithRole = role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${membersWithRole.join(', ')}`);

}
I do that but it just says users: and no actual members
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
nBux
nBuxOP16mo ago
like this?
if (queueCount1v1>1) {
const guild = interaction.guild;
const role = guild.roles.cache.get(Queued1v1Role);
const membersWithRole = role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${membersWithRole.join(', ')}`);

}
if (queueCount1v1>1) {
const guild = interaction.guild;
const role = guild.roles.cache.get(Queued1v1Role);
const membersWithRole = role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${membersWithRole.join(', ')}`);

}
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs16mo ago
method GuildMemberManager#fetch() Fetches member(s) from a guild.
nBux
nBuxOP16mo ago
Sorry, here
if (queueCount1v1>1) {
const guild = interaction.guild;
guild.members.fetch()
const role = guild.roles.cache.get(Queued1v1Role);
const membersWithRole = role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${membersWithRole.join(', ')}`);

}
if (queueCount1v1>1) {
const guild = interaction.guild;
guild.members.fetch()
const role = guild.roles.cache.get(Queued1v1Role);
const membersWithRole = role.members.map(member => member.displayName);
console.log(`Game starting up!\nUsers: ${membersWithRole.join(', ')}`);

}
grass
grass16mo ago
GuildMemberManager.fetch() returns a promise, resolve it
Want results from more Discord servers?
Add your server