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 toolkit6mo 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
edomangoOP6mo ago
npm list discord.js [email protected] /home/edoardo/Desktop/Projects/Palermo/Volley └── [email protected] 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
edomangoOP6mo ago
No description
edomango
edomangoOP6mo ago
and this is the output
Suca | 21
C: @edomango
CV: <@521085980451799079>
2
1
Suca | 21
C: @edomango
CV: <@521085980451799079>
2
1
monbrey
monbrey6mo ago
I don't think fetching a message will populate those users into cache Only the messageCreate event does
edomango
edomangoOP6mo 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