Trying to get users displayName, returning unknown user

Sorry if this seems obvious - still finding my feet with d.js. I want to display a leaderboard of the top 10 users within logging.json, however, when the command is run all users display as 'Unknown user' apart from the author (check attached)
const loggingData = JSON.parse(fs.readFileSync("logging.json", "utf8"));
const users = Object.keys(loggingData).sort(function(a, b) {
return loggingData[b].tweets - loggingData[a].tweets;
}).slice(0, 10);
if (users.length == 0) {
await interaction.reply("No tweets logged");
} else {
const leaderboard = new EmbedBuilder()
.setTitle("Tweet Leaderboard")
.setColor("#54a329")
.setFooter({
text: "redacted",
iconURL: "redacated",
})
.setTimestamp();
for (let i = 0; i < users.length; i++) {
let user = users[i];
let position = i + 1;
let numTweets = loggingData[user].tweets;
let member = interaction.guild.members.cache.get(user);
let displayName = member ? member.displayName : "Unknown User";
leaderboard.addFields({
name: position + ". " + "<@" + displayName + ">",
value: numTweets + " tweets logged.",
inline: false
});
}
await interaction.reply({
embeds: [leaderboard]
});
const loggingData = JSON.parse(fs.readFileSync("logging.json", "utf8"));
const users = Object.keys(loggingData).sort(function(a, b) {
return loggingData[b].tweets - loggingData[a].tweets;
}).slice(0, 10);
if (users.length == 0) {
await interaction.reply("No tweets logged");
} else {
const leaderboard = new EmbedBuilder()
.setTitle("Tweet Leaderboard")
.setColor("#54a329")
.setFooter({
text: "redacted",
iconURL: "redacated",
})
.setTimestamp();
for (let i = 0; i < users.length; i++) {
let user = users[i];
let position = i + 1;
let numTweets = loggingData[user].tweets;
let member = interaction.guild.members.cache.get(user);
let displayName = member ? member.displayName : "Unknown User";
leaderboard.addFields({
name: position + ". " + "<@" + displayName + ">",
value: numTweets + " tweets logged.",
inline: false
});
}
await interaction.reply({
embeds: [leaderboard]
});
2 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
monbrey
monbrey2y ago
These members are not in the cache, you'll need to fetch them
Want results from more Discord servers?
Add your server