How do I get the referenced user id from a referenced message?

let prevMessages = await message.channel.messages.fetch({ limit: 15 });
prevMessages.reverse();
await message.channel.sendTyping()

prevMessages.forEach((msg) => {
if (msg.author.id === message.author.id) {
conversationLog.push({
"role": "user",
"content": msg.content
});
} else if (msg.author.bot && /* is a reference to a message sent by the msg.author*/) {
conversationLog.push({
"role": "assistant",
"content": msg.content
});
}
});
let prevMessages = await message.channel.messages.fetch({ limit: 15 });
prevMessages.reverse();
await message.channel.sendTyping()

prevMessages.forEach((msg) => {
if (msg.author.id === message.author.id) {
conversationLog.push({
"role": "user",
"content": msg.content
});
} else if (msg.author.bot && /* is a reference to a message sent by the msg.author*/) {
conversationLog.push({
"role": "assistant",
"content": msg.content
});
}
});
Heres my code
10 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.
lupus
lupus2y ago
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.
Trolli
TrolliOP2y ago
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
lupus
lupus2y ago
Oh, you mean the message it replied to?
Trolli
TrolliOP2y ago
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
lupus
lupus2y ago
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.
d.js docs
d.js docs2y ago
method Message#fetchReference() Fetches the Message this crosspost/reply/pin-add references, if available to the client
lupus
lupus2y ago
Note that the function is an async function and it could throw an error if there's no message reference
Trolli
TrolliOP2y ago
alr 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:
let prevMessages = await message.channel.messages.fetch({ limit: 15 });
for (const msg of prevMessages) {
if (isUser) {
if (msg.author.id === message.author.id) {
... }
let prevMessages = await message.channel.messages.fetch({ limit: 15 });
for (const msg of prevMessages) {
if (isUser) {
if (msg.author.id === message.author.id) {
... }
I just get an error that says that it doesnt have an id TypeError: Cannot read properties of undefined (reading 'id')
lupus
lupus2y ago
prevMessages is of type Collection<Snowflake, Message>. To loop over all messages, use for (const msg of prevMessages.values())
Want results from more Discord servers?
Add your server