Skip to main content

Snowflake

Introduction

There is often a need to get a unique ID for entities, be that for Discord messages/channels/servers, keys in a database or many other similar examples. There are many ways to get such a unique ID, and one of those is using a so-called "snowflake". You can read more about snowflake IDs in this Medium article.

Installation

npm install @sapphire/snowflake

Usage

Constructing a Snowflake
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');

// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');

// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);

// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();
Constructing a Discord Snowflake
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');

// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.generate();
Constructing a Twitter Snowflake
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');

// Generate a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.generate();
Deconstructing Snowflake
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');

// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');

// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);

// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');
Deconstructing Discord Snowflake
const { DiscordSnowflake } = require('@sapphire/snowflake');

// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
Deconstructing Twitter Snowflake
const { TwitterSnowflake } = require('@sapphire/snowflake');

// Generate a snowflake with Discord's epoch
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');