I would like to list server members on Discord using discord.js library. However, some (mostly inactive) users are displayed in form <@123456789123456789> (their id) instead of @Michal (their clickable name with user profile).
It can be because some members are uncached? I tried fetch all members before printing them, even with force option. But without success.
const executeCommand = async (client, message) => {
const members = await message.guild.members.fetch({ force: true })
message.channel.send({ embed: {
title: 'Members',
description: members.map(m => `<@${m.id}>`).join(', ')
} })
}
Also both Presence intent and Server member intent are enabled on my bot. How can I fix it?
This is unfortunately not possible as the fact that the mentions show up as IDs and not the clickable usernames is actually an issue with the Discord client and not the bot itself but does indeed occur when the users haven't been fetched. If you restart your client, look at a message the person in question has sent, and then return to the message sent by the bot the username will show up and it's fully possible that what appears as just the ID for you shows up properly for someone else. If anything, this is something that has to be fixed by Discord themselves and out of our control as bot developers.
I don't know what causes it, but some mentions start with <@! instead of just <@.
I would recommend changing that map function to members.map(m => m.toString()), as you can see in docs, toString() method returns member's mention string.
https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=toString