Asynchronous user fetching causing issues with embeds and buttons

Hi, I'm using redis to track the number of messages a user has sent in a thread and I store it in a hash using the userid as the key. I'm trying to get the user's name to use it as the label for a button. My code goes over an array of IDs and for each ID it grabs the username and adds a button to an actionrow. The cache is undefined for some reason so I'm using interaction.guild?.members.fetch(<user id>). The issue being that awaiting that method causes some weird issues (see the pastebin) and not awaiting it returns nothing as expected. The function it's in is async... https://pastebin.com/t4sdXMTp
26 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.
Poulette
PouletteOP2y ago
discord.js 14.11.0 node 16 LTS Here's a code snippet
if (helpers.length > 0) {

const row = new ActionRowBuilder<ButtonBuilder>()
logger.debug(`Helpers: ${helpers}`)

for (const helper of helpers) {
const user = await interaction.guild?.members.fetch(helper)
row.addComponents(new ButtonBuilder({ customId: `${this.name}.${helper}`, label: `${user.username}`, style: ButtonStyle.Success, emoji: `👍` }))
}
embed.setDescription(helpers.length > 1 ? 'It looks like these users helped you, do you want to give them kudos?' : 'It looks like this user helped you, do you want to give them kudos?')
await interaction.reply({ embeds: [embed], components: [row], fetchReply: true })
}
if (helpers.length > 0) {

const row = new ActionRowBuilder<ButtonBuilder>()
logger.debug(`Helpers: ${helpers}`)

for (const helper of helpers) {
const user = await interaction.guild?.members.fetch(helper)
row.addComponents(new ButtonBuilder({ customId: `${this.name}.${helper}`, label: `${user.username}`, style: ButtonStyle.Success, emoji: `👍` }))
}
embed.setDescription(helpers.length > 1 ? 'It looks like these users helped you, do you want to give them kudos?' : 'It looks like this user helped you, do you want to give them kudos?')
await interaction.reply({ embeds: [embed], components: [row], fetchReply: true })
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
nope I tried a bunch of things and in every case it's either undefined or it gives the same error
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
I thought about it but haven't tried tbh
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
it's a key so they're strings
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
I'll try that I probably tried it already though but who knows give it a sec teamcity needs to build the image Either my Types are messed up somehow or TS is dumb but it keeps telling me there is no displayname anywhere to be found
d.js docs
d.js docs2y ago
property GuildMember#displayName The nickname of this member, or their username if they don't have one
Poulette
PouletteOP2y ago
it's saying client.users.fetch returns a User not a GuildMember
Poulette
PouletteOP2y ago
d.js docs
d.js docs2y ago
Despite sounding similar there is a distinct difference between users and members in Discord: • User: global Discord user data (global avatar, username, tag, id) • GuildMember: user data associated to a guild (guild, nickname, roles, voice, guild avatar, etc.) • Conversion: User ➞ GuildMember | GuildMember ➞ User * Note: Events received in cached guilds will often have both the member and user available, eg. interaction.user and interaction.member
Poulette
PouletteOP2y ago
I see nice bot btw I've changed the var names so it's not as weird
const name = interaction.guild?.members.cache.get(helper)?.displayName
row.addComponents(new ButtonBuilder({ customId: `${this.name}.${helper}`, label: `${name}`, style: ButtonStyle.Success, emoji: `👍` }))
const name = interaction.guild?.members.cache.get(helper)?.displayName
row.addComponents(new ButtonBuilder({ customId: `${this.name}.${helper}`, label: `${name}`, style: ButtonStyle.Success, emoji: `👍` }))
and it's undefined ah fuk wrong methods it shouldn't be undefined though I know the user is in the guild though, do they need to speak or smth to be added to the cache ? oh that's cool yeah I checked all that that's cause my brain is small and I used the wrong method
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
and since it takes forever for docker to build the image I can only sit here and watch as my stupid code builds
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
I'm testing locally using docker
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
it's a complex stack I can't just run the bot anyways it works now thanks guys I guess I tried eveything but that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
const name = await interaction.guild?.members.fetch(helper)
row.addComponents(new ButtonBuilder({ customId: `${this.name}.${helper}`, label: `${name?.displayName}`, style: ButtonStyle.Success, emoji: `👍` }))
const name = await interaction.guild?.members.fetch(helper)
row.addComponents(new ButtonBuilder({ customId: `${this.name}.${helper}`, label: `${name?.displayName}`, style: ButtonStyle.Success, emoji: `👍` }))
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Poulette
PouletteOP2y ago
it's really cool you should look into it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server