Bug of discord.js Message.mentions.members . Doesn't include all mentioned members.

I am fetching a message by id and I am trying to read mentioned members in the message. The problem is it doesn't return all mentioned members.
6 Replies
d.js toolkit
d.js toolkit2mo 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!
edomango
edomango2mo ago
npm list discord.js volleyball@1.0.0 /home/edoardo/Desktop/Projects/Palermo/Volley └── discord.js@14.15.3 node -v v20.5.1 Here is the code:
const message = await interaction.channel?.messages.fetch(messageReference)
if (!message) return console.log("no message");

const players = []
console.log(message.content)
console.log(message.mentions.users?.size)
console.log(message.mentions.members?.size)

for(const [id, member] of message.mentions.members!) {
players.push({
name:member.nickname || member.displayName,
id:id
})
}
const message = await interaction.channel?.messages.fetch(messageReference)
if (!message) return console.log("no message");

const players = []
console.log(message.content)
console.log(message.mentions.users?.size)
console.log(message.mentions.members?.size)

for(const [id, member] of message.mentions.members!) {
players.push({
name:member.nickname || member.displayName,
id:id
})
}
Here is the output:
Suca | 21
C: <@&1263598098652074139>
CV: <@521085980451799079>
CC: @edomango
2
1
Suca | 21
C: <@&1263598098652074139>
CV: <@521085980451799079>
CC: @edomango
2
1
I have also notices that it doesn't recognise a user inside my server as a guild member I have also tried changing the message
edomango
edomango2mo ago
No description
edomango
edomango2mo ago
and this is the output
Suca | 21
C: @edomango
CV: <@521085980451799079>
2
1
Suca | 21
C: @edomango
CV: <@521085980451799079>
2
1
monbrey
monbrey2mo ago
I don't think fetching a message will populate those users into cache Only the messageCreate event does
edomango
edomango2mo ago
How can I solve this Like should I consider users collection and then fetch for each user the guild member? Doesn’t seem rate limit friendly if I increase users mentions There could be 16 mentions Already mentioned So what should I do 😭 Wow didn’t know I could fetch multiple users Thank you very much I’m changing the code And I let u know if it worked For a server of almost 30 k members I shouldn't cache right ? It works , but it doesn't keep the order of mentions Pls tell me there is a way other than using members... I have also tried with regex , but fetching changes the order
const membersMentioned = await interaction.guild!.members.fetch({ user: [...message.content.matchAll(/<@!?(\d+)>/g)].map(match => match[1]) })
const membersMentioned = await interaction.guild!.members.fetch({ user: [...message.content.matchAll(/<@!?(\d+)>/g)].map(match => match[1]) })
I've verified that
[...message.content.matchAll(/<@!?(\d+)>/g)].map(match => match[1])
[...message.content.matchAll(/<@!?(\d+)>/g)].map(match => match[1])
is ordered But fetch changes the order
const orderedMentions = [...message.content.matchAll(/<@!?(\d+)>/g)].map(match => match[1])
const membersMentioned = await interaction.guild!.members.fetch({ user: orderedMentions })

if(!membersMentioned) return;

for(const memberId of orderedMentions) {
const member = membersMentioned.get(memberId)
if (!member) return;
players.push({
name:member.nickname || member.displayName,
id:memberId
})
}
const orderedMentions = [...message.content.matchAll(/<@!?(\d+)>/g)].map(match => match[1])
const membersMentioned = await interaction.guild!.members.fetch({ user: orderedMentions })

if(!membersMentioned) return;

for(const memberId of orderedMentions) {
const member = membersMentioned.get(memberId)
if (!member) return;
players.push({
name:member.nickname || member.displayName,
id:memberId
})
}
solved but I don't know if it is overkill
Want results from more Discord servers?
Add your server