Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the content of a pinned message discord.js

I am making a Discord bot in discord.js and I want to get the content of a pinned message.

I know I can use interaction.channel.messages.fetchPinned().then(pinnedMessages => /* do stuf */ ); to fetch the messages. But if you try to print out the value of pinnedMessages.content it will return undefined.

The type of pinnedMessages is an object, but I do not know how I can get the contents of it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, how do I do this?

>Solution :

fetchPinned returns a collection of messages. There are a couple of methods you can use, depending on your needs.

To get the content of the first pinned message, you can use:

let messages = await interaction.channel.messages.fetchPinned()
let firstMessage = messages.first()

console.log(firstMessage.content)

To get a pinned message by its ID:

let messages = await interaction.channel.messages.fetchPinned()
let messageById = messages.get('MESSAGE ID HERE')

console.log(messageById.content)

If you want to log all pinned messages’ content:

let messages = await interaction.channel.messages.fetchPinned()

messages.forEach(msg => {
  console.log(msg.content)
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading