Search code examples
datedatetimediscord

Discord Client's Search Timestamp


I'm making a simple HTTP request to Discord's API to retrieve messages within a guild:

https://discord.com/api/v9/guilds/<guildId>/messages/search

This endpoint is used by the actual client, not bots, so I can't figure out what kind of timestamp they are using in the date parameters which are min_id and max_id. The values are numeric they don't appear to be epoch.

Steps to recreate:

  1. Login to Discord using any regular user account
  2. Go to any text channel (direct message, group, guild channel)
  3. Open developer tools and go to the network tab
  4. Use the search box to find the HTTP request being sent to retrieve messages (before, after, during)

You can see the API request contains the parameters channel_id and min_id when the selected option is after, or max_id when the option is before, else both.

The min/max values are timestamp ranges in the message ID. This information is not in the documentation so I'm looking for clarification as to what these values are in order use them in my own project to interact with the API.


Solution

  • The timestamps used by the actual Discord client work off the Discord Epoch timestamp which is similar to the normal unix timestamp except that it starts off from the 1st of January, 2015 or 1420070400000 inclusive of milliseconds.

    All Discord messages (and other objects that require unique identifiers) use Snowflakes to generate a DateTime (ISO8601) for when the message was created:

    Convert Discord Snowflake to DateTime

    Discord do provide an example on how to generate the snowflake ID that you're seeing in the Discord client's request in their documentation:

    (timestamp_ms - DISCORD_EPOCH) << 22
    

    You may also be interested in their documentation which outlines how they use snowflakes in conjunction with pagination for retrieving messages with the before and after parameters you're seeing:

    We typically use snowflake IDs in many of our API routes for pagination. The standardized pagination paradigm we utilize is one in which you can specify IDs before and after in combination with limit to retrieve a desired page of results. You will want to refer to the specific endpoint documentation for details.

    It is useful to note that snowflake IDs are just numbers with a timestamp, so when dealing with pagination where you want results from the beginning of time (in Discord Epoch, but 0 works here too) or before/after a specific time you can generate a snowflake ID for that time.