Get last message from text channel with discord.js

grafpatron

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 right after my bot started and use the command on it, but if I restart my bot it seems to get the wrong message. Also, what about messages that are older than 14 days?

I can't really provide code because it's just one line, like:

const message = msg.channel.messages. ...

Zsolt Meszaros

You can fetch the last two messages in the channel using message.channel.messages.fetch({ limit: 2 }). It contains the command to fetch and the message right before that. You can now grab the last message by using the .last() method on the returned collection:

const messages = await message.channel.messages.fetch({ limit: 2 });
const lastMessage = messages.last();

console.log(lastMessage.content);

enter image description here

PS.: It has no problem with fetching messages older than 14 days.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I get a list of all the messages in a channel and then pick a random message from that list to send as a message?

From Java

message.channel.id Discord PY

From Java

How to remove permission from channel for role - discord.js?

From Dev

Get last message from kafka consumer console script

From Dev

Get last message from each conversation

From Dev

Get Last Message from Each Conversation sql

From Dev

SQL - Get last message from each conversation

From Dev

how to pull message data from discord.js?

From Dev

discord channel link in message

From Dev

How do I send a message to a specific channel in discord.js?

From Dev

Forwarding message from group or channel

From Dev

Check if a user can send message in a specific channel discord.js

From Dev

Get the channel deletor discord py

From Dev

Discord.JS getting all attachments from a message

From Dev

How to get the last 5-10 message from a database?

From Dev

Get Last Message from Each Conversation sql

From Dev

Get the last message from each conversation

From Dev

discord channel link in message

From Dev

Get last Message Reply From Conversation

From Dev

Sending Message to Discord Channel without message from user

From Dev

discord.py How to get logs from a DM channel

From Dev

Pop message from channel in Redis

From Dev

Discord.js - How to spam messages in a text channel?

From Dev

message.channel.send not working discord.js

From Dev

Discord Bot shall delete "user pinned message to channel" message

From Dev

How to get rid of Text Channels from channel list in Discord.py

From Dev

Get the Emoji url from a message in discord.js

From Dev

Discord.js with discord.js-commando Prevent command from running in specific channel

From Dev

Get emoji informations from a message in discord.js

Related Related

  1. 1

    How can I get a list of all the messages in a channel and then pick a random message from that list to send as a message?

  2. 2

    message.channel.id Discord PY

  3. 3

    How to remove permission from channel for role - discord.js?

  4. 4

    Get last message from kafka consumer console script

  5. 5

    Get last message from each conversation

  6. 6

    Get Last Message from Each Conversation sql

  7. 7

    SQL - Get last message from each conversation

  8. 8

    how to pull message data from discord.js?

  9. 9

    discord channel link in message

  10. 10

    How do I send a message to a specific channel in discord.js?

  11. 11

    Forwarding message from group or channel

  12. 12

    Check if a user can send message in a specific channel discord.js

  13. 13

    Get the channel deletor discord py

  14. 14

    Discord.JS getting all attachments from a message

  15. 15

    How to get the last 5-10 message from a database?

  16. 16

    Get Last Message from Each Conversation sql

  17. 17

    Get the last message from each conversation

  18. 18

    discord channel link in message

  19. 19

    Get last Message Reply From Conversation

  20. 20

    Sending Message to Discord Channel without message from user

  21. 21

    discord.py How to get logs from a DM channel

  22. 22

    Pop message from channel in Redis

  23. 23

    Discord.js - How to spam messages in a text channel?

  24. 24

    message.channel.send not working discord.js

  25. 25

    Discord Bot shall delete "user pinned message to channel" message

  26. 26

    How to get rid of Text Channels from channel list in Discord.py

  27. 27

    Get the Emoji url from a message in discord.js

  28. 28

    Discord.js with discord.js-commando Prevent command from running in specific channel

  29. 29

    Get emoji informations from a message in discord.js

HotTag

Archive