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 toolkit17mo 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
grass17mo ago
assuming the guild's members are cached, you can use <Role>.members and map those by nickname
nBux
nBuxOP17mo ago
How do I do that
grass
grass17mo ago
you can fetch the members with <Role>.guild.members.fetch(), then use <Role>.members.map()
d.js docs
d.js docs17mo 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
grass17mo ago
you can map by GuildMember.nickname
nBux
nBuxOP17mo ago
What would the code look like?
grass
grass17mo ago
noSpoons explaining how you can
nBux
nBuxOP17mo 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
grass17mo ago
sure
nBux
nBuxOP17mo ago
const members = Queued1v1Role.members.map(member => member.displayName); that doesnt work tho
grass
grass17mo ago
what do you mean doesnt work
nBux
nBuxOP17mo ago
I get this TypeError: Cannot read properties of undefined (reading 'map')
grass
grass17mo ago
show how you define Queued1v1Role
nBux
nBuxOP17mo ago
const Queued1v1Role = '1135435838046621756';
grass
grass17mo 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
nBuxOP17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
nBux
nBuxOP17mo 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 User17mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs17mo ago
method GuildMemberManager#fetch() Fetches member(s) from a guild.
nBux
nBuxOP17mo 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
grass17mo ago
GuildMemberManager.fetch() returns a promise, resolve it
Want results from more Discord servers?
Add your server