Get last message from text channel with discord.js

I feel like all my research on this topic lead me only to outdated solutions.

With my Discord.js bot, I have a command. For it to work I need to get the last message in a channel right before the command. I am struggling with all those fetches and partials and cache etc. Sometimes it works when I post the message when after my bot started and use the command on it, but if I restart my bot it seems to get the wrong message I dunno. Also, what about messages that are older than 14 days?
I cant really provide code because its just one line, like:

const message = msg.channel.messages.blabla

Thanks in advance!

96 thoughts on “Get last message from text channel with discord.js”

Leave a Comment