harmony/src/utils/snowflake.ts

33 lines
630 B
TypeScript
Raw Normal View History

/** Utility class to extract data from a Snowflake (Discord ID) */
export class Snowflake {
2020-12-03 04:06:41 +00:00
id: string
2020-12-02 12:29:52 +00:00
constructor(id: string) {
2020-12-03 04:06:41 +00:00
this.id = id
}
get snowflake(): bigint {
return BigInt.asUintN(64, BigInt(this.id))
}
get timestamp(): number {
2021-01-25 13:26:32 +00:00
return Number((this.snowflake >> 22n) + 1420070400000n)
}
get workerID(): number {
2021-01-25 13:26:32 +00:00
return Number((this.snowflake & 0x3e0000n) >> 17n)
}
get processID(): number {
2021-01-25 13:26:32 +00:00
return Number((this.snowflake & 0x1f00n) >> 12n)
}
get increment(): number {
2021-01-25 13:26:32 +00:00
return Number(this.snowflake & 0xfffn)
}
2021-01-24 18:49:08 +00:00
get toString(): string {
return this.id
}
}