Home>

Language: JavaScript
Library: discord.js
Discord bot is trying to get all messages within a specific period in a specific server where the bot is participating.

In fetchMessages function
https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=fetchMessages
Since only 100 items can be acquired, we are trying to acquire it repeatedly while shifting the period using the before and after conditions together.

Error message It seems that after condition becomes invalid when

before condition is specified.

Applicable source code
client.on ('message', message =>
{
  if (message.content.match (/ ^ test $/)) {
    let channel = message.channel;
    channel.fetchMessages ({limit: 100, before: discord.SnowflakeUtil.generate (new Date (2019, 5, 2, 0, 0, 0)), after: discord.SnowflakeUtil.generate (new Date (2019, 5, 1 , 0, 0, 0))})
      .then (messages =>console.log (`Received ${messages.size} messages`))
      .catch (console.error);
}


* Because the code is still in the test stage of fetchMessages, the period has not changed

It turns out that if after

before is not specified, the after condition is valid and messages after the specified date and time can be obtained.
Even if the order of before and after was changed, only before was valid and the after condition was invalid.

Search first
https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=search
I tried to use, but it doesn't seem to work with a bot account ...

Supplemental information (FW/tool version etc.)

discord.js 11.5.1

  • Answer # 1

    Before and after cannot be used at the same time as written in the document

    If you do something like this, you can usebeforeto get the message, andbeforeto get and repeat from that last message.

Related articles