lastmessageID always null except for me

Viewed 170

Presentation

Goal: Find when the last message of a designated was send

How: Use property of User, lastmessageID, fetch the message corresponding to this ID to find the property createdTimestamp

Problem: I'm the only one in the was doesn't have the property lastmessageID equal to null even if the user had send message a few minute ago

Question: Is it coming from my coding or from Discord.js ? Is there a way to bypass this problem ?

NOTE: I know it's cached:

  • When someone send a message while the bot is alive give the same result.

  • For the sake of the test, I summon a bot that so I give me a"fresh" message when testing

CODE SAMPLE

every "..." is to protect personal data

SCRIPT

  function findID() {
    var IDarray = userm.users.map((user) => user.lastMessageID)
    console.log("IDarray: ", IDarray)
    var ID = IDarray[0]
    console.log("ID: ", ID)
    msg(ID)
  }

  function msg(lastmsgID) {
    var lastmsg = message.channel.messages.fetch(lastmsgID).then(message => time(message)).catch(console.error);
  }

  function time(msg){
    ...
  }

  findID();


RESULTS WITH ME

user:  [
  User {
    id: '...',
    bot: false,
    username: '...',
    discriminator: '....',
    avatar: '...',
    flags: UserFlags { bitfield: ... },
    lastMessageID: '...',
    lastMessageChannelID: '...'
  }
]
IDarray:  [ '...' ]
ID:  ...
<Wed Oct 07 2020 00:30:52 GMT+0200 (GMT+02:00)> : Recieved Message to ..., content /test <@!...>
Date as YYYY-MM-DD hh:mm:ss ==> 2020-10-07 00:30:54

RESULTS WITH ANYONE

user:  [
  User {
    id: '...',
    bot: false,
    username: '...',
    discriminator: '...',
    avatar: '...',
    lastMessageID: null,
    lastMessageChannelID: null,
    flags: UserFlags { bitfield: ... }
  }
]
IDarray:  [ null ]
ID:  null
<Wed Oct 07 2020 00:26:44 GMT+0200 (GMT+02:00)> : Recieved Message to ..., content /test <@...>
Date as YYYY-MM-DD hh:mm:ss ==> NaN-aN-aN aN:aN:aN

If you think I can improve this question by anyway, don't hesitate to tell me ! Thanks for reading !

2 Answers

Try client.users.fetch('uid'); to fetch the user then pull the info you need. This pulls updates the user from Discord not from the cache.

This is a problem with the cache. The bot will only give lastMessage if the message was sent after the bot is started. You would need to fetch all the messages from all the channels for it to work, or just wait for the users to post a message after to bot is started.

Related