How do I get the referenced user id from a referenced message?
Heres my code
10 Replies
• 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.I'm unsure what you want to know - you can get the user id of a message by using
<Message>.author.id
as you also did before in your code.I use the openai api, and want to provide a bit more context to the answers. Because of that I want add the assistant role to the conversation log so that the bot doesnt repeat answers. What im trying to find out is how to get the referenced user of a message (like in the picture) to add the response to the conversation log. So that it only adds the replies of the conversation with the certain user to the log. (sorry for bad english) i hope this makes sense
Oh, you mean the message it replied to?
I want the user id of the original message that has been replied to, so that i can check if it isn't an other user
Alright, let me check the documentation
You want to fetch the referenced Message with
<Message>.fetchReference()
which returns a Message object if there's a reference. Of that object you can check the .author.id
to get the author id. Message#fetchReference()
Fetches the Message this crosspost/reply/pin-add references, if available to the client
Note that the function is an
async
function and it could throw an error if there's no message referencealr ima try that
tried it and works ty ❤️ . One other question tho, do you know why I can't get the msg.author.id in this rewritten code:
I just get an error that says that it doesnt have an id
TypeError: Cannot read properties of undefined (reading 'id')
prevMessages
is of type Collection<Snowflake, Message>
. To loop over all messages, use for (const msg of prevMessages.values())