Is there a way in Discord.js to unpin all pinned messages?

Viewed 464

I am currently working on my first Discord bot and I wanted to unpin all pinned messages in a specific channel. However, I have no idea how to get all the pinned messages.

I tried to use message.channel.messages.fetchPinned().array() to get an array of all pinned messages, but then I get an error that fetchPinned().array() is not a function. What am I doing wrong?

1 Answers

fetchPinned returns a promise, so you need to wait for it to be resolved first. Once it's resolved, it returns a collection of messages, so you can iterate over them using the each method.

message.channel.messages
  .fetchPinned()
  .then((pinnedMessages) => {
    pinnedMessages.each((msg) => msg.unpin().catch(console.error));
  })
  .catch(console.error);
Related