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 classimport { Snowflake } from '@sapphire/snowflake';// Define a custom epochconst epoch = new Date('2000-01-01T00:00:00.000Z');// Create an instance of Snowflakeconst snowflake = new Snowflake(epoch);// Generate a snowflake with the given epochconst uniqueId = snowflake.generate();
Constructing a Discord Snowflake
// Import the Snowflake classimport { DiscordSnowflake } from '@sapphire/snowflake';// Generate a snowflake with Discord's epochconst uniqueId = DiscordSnowflake.generate();
Constructing a Twitter Snowflake
// Import the Snowflake classimport { TwitterSnowflake } from '@sapphire/snowflake';// Generate a snowflake with Twitter's epochconst uniqueId = TwitterSnowflake.generate();
Deconstructing Snowflake
// Import the Snowflake classimport { Snowflake } from '@sapphire/snowflake';// Define a custom epochconst epoch = new Date('2000-01-01T00:00:00.000Z');// Create an instance of Snowflakeconst snowflake = new Snowflake(epoch);// Deconstruct a snowflake with the given epochconst uniqueId = snowflake.deconstruct('3971046231244935168');
Deconstructing Discord Snowflake
import { DiscordSnowflake } from '@sapphire/snowflake';// Generate a snowflake with Discord's epochconst uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
Deconstructing Twitter Snowflake
import { TwitterSnowflake } from '@sapphire/snowflake';// Generate a snowflake with Discord's epochconst uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');