2021-01-25 11:28:46 +00:00
|
|
|
/** Utility class to extract data from a Snowflake (Discord ID) */
|
2020-10-24 15:00:42 +00:00
|
|
|
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))
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 11:28:46 +00:00
|
|
|
get timestamp(): number {
|
2021-01-25 13:26:32 +00:00
|
|
|
return Number((this.snowflake >> 22n) + 1420070400000n)
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 11:28:46 +00:00
|
|
|
get workerID(): number {
|
2021-01-25 13:26:32 +00:00
|
|
|
return Number((this.snowflake & 0x3e0000n) >> 17n)
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 11:28:46 +00:00
|
|
|
get processID(): number {
|
2021-01-25 13:26:32 +00:00
|
|
|
return Number((this.snowflake & 0x1f00n) >> 12n)
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 11:28:46 +00:00
|
|
|
get increment(): number {
|
2021-01-25 13:26:32 +00:00
|
|
|
return Number(this.snowflake & 0xfffn)
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
2021-01-24 18:49:08 +00:00
|
|
|
|
|
|
|
get toString(): string {
|
|
|
|
return this.id
|
|
|
|
}
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|