Last updated
Discord Message IDs
Every Discord message has a unique Snowflake ID that encodes the exact millisecond
it was sent. This is useful for archiving conversations, finding messages from a
specific time period, and using Discord's API to fetch messages around a specific timestamp.
The Discord API accepts Snowflake IDs in the before, after,
and around parameters of the message fetch endpoint.
Finding Messages by Date
const DISCORD_EPOCH = 1420070400000n;
// Convert a date to a Discord Snowflake ID
// Useful for fetching messages before/after a specific time
function dateToSnowflake(date) {
const ms = BigInt(date.getTime()) - DISCORD_EPOCH;
return String(ms << 22n);
}
// Decode a message ID to get its timestamp
function decodeMessageId(messageId) {
const id = BigInt(messageId);
const ms = Number((id >> 22n) + DISCORD_EPOCH);
return {
messageId,
createdAt: new Date(ms).toISOString(),
timestamp: ms
};
}
// Fetch messages from a specific date using Discord API
async function fetchMessagesFromDate(channelId, date, token) {
const snowflake = dateToSnowflake(date);
const response = await fetch(
`https://discord.com/api/v10/channels/${channelId}/messages?after=${snowflake}&limit=100`,
{ headers: { Authorization: `Bot ${token}` } }
);
return response.json();
}
Message ID Use Cases
- Archiving: Record message IDs to reconstruct conversation timelines.
- Moderation: Find messages sent during a specific incident window.
- Bots: Use
afterparameter to fetch only new messages since last check. - Analytics: Count messages per time period by decoding ID timestamps.