I have been programming a discord bot, following tutorials as needed since I am quite new, but I have come across a problem that, as simple as it sounds, I can’t seem to solve.
I am trying to draw a leaderboard, and for each placement on the leaderboard I want to get the user’s avatar to draw onto the leaderboard. To do this, I want to be able to get the user through just the UserId. However, I am doing all of this in a separate slash command. Here is a very simplified version of what I’m trying to do to give some context and/or maybe a better explanation:
const { SlashCommandBuilder, AttachmentBuilder } = require('discord.js');
// ... some functions and what not go here
module.exports = {
//... lines for setting up the command go here
async execute(interaction){
const data = fetchData(); //A function that gets the whole list of data; did not put it in the codeblock to save on space since it's not necessary
// ... a bunch of stuff, ordering my data, etc.
data.forEach(async (userData, index) => {
// And here begins my issue. The line below is just what I have attempted to do
const target = interaction.guild.members.cache.get(userData.UserId);
});
};
}
I have done a fair bit of research, and the only feasible solution I could find (which you can see is in the above example code) is to do
const target = interaction.guild.members.cache.get("User_ID");
However, even though I can log the return in the console, if I tried to do something like "target.user" it would say target is undefined. And, in case it helps, yes, I do have GuildMembers in my intents.
2
Answers
If you just want to get the id of the user that ran the slash command, you can use
interaction.user.id
.To get a user by id, you can run:
You need to use
members.fetch
since it’s async you need to change yourforEach
toforof
becauseforEach
is synchronous