This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving to unified ids to support multiple platforms integration
- Loading branch information
Hatry1337
committed
Apr 25, 2023
1 parent
8f8838b
commit 9fc2f18
Showing
17 changed files
with
286 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| Unified ID Binary Structure | | ||
+--------------------------------------------------------------------+ | ||
| 64 20 15 0 | | ||
| 11111111111111111111111111111111111111111111 11111 111111111111111 | | ||
+--------------------------------------------------------------------+ | ||
+------------------------------------------------------------------------------------------+ | ||
| Unified ID components description | | ||
+---------+--------------------------------------------------------------------------------+ | ||
| [64-20] | Milliseconds from RainbowBOT Epoch (April 28 of 2019 TZ=GMT+0 - 1556409600000) | | ||
+---------+--------------------------------------------------------------------------------+ | ||
| [20-15] | Associated data type: | | ||
| | 0 - Reserved | | ||
| | 1 - Any data | | ||
| | 2 - User | | ||
| | 3 - Discord Guild | | ||
| | 4 - Discord Channel | | ||
| | 5 - Other channel | | ||
| | ... | | ||
+---------+--------------------------------------------------------------------------------+ | ||
| [15-00] | Increment number | | ||
+---------+--------------------------------------------------------------------------------+ | ||
*/ | ||
|
||
export const RainbowBOTEpoch = 1556409600000; | ||
|
||
export enum UnifiedIdDataType { | ||
RESERVED = 0, | ||
AnyData = 1, | ||
User = 2, | ||
DiscordGuild = 3, | ||
DiscordChannel = 4, | ||
OtherChannel = 5, | ||
} | ||
|
||
export class UnifiedId { | ||
private static increment = 0; | ||
public static generate(dataType: UnifiedIdDataType = UnifiedIdDataType.AnyData, milliseconds?: number, increment?: number) { | ||
if(milliseconds === undefined) { | ||
milliseconds = new Date().getTime() - RainbowBOTEpoch; | ||
} | ||
if(increment === undefined) { | ||
UnifiedId.increment++; | ||
if(UnifiedId.increment >= 32768) { | ||
UnifiedId.increment = 0; | ||
} | ||
increment = UnifiedId.increment; | ||
} | ||
|
||
return BigInt(increment) + | ||
(BigInt(dataType) << 15n) + | ||
(BigInt(milliseconds) << 20n) | ||
} | ||
|
||
public static parse(id: bigint) { | ||
let milliseconds = id >> 20n; | ||
let dataType = (id - (milliseconds << 20n)) >> 15n; | ||
let increment = (id - (milliseconds << 20n) - (dataType << 15n)); | ||
|
||
return { | ||
milliseconds: Number(milliseconds), | ||
dataType: Number(dataType) as UnifiedIdDataType, | ||
increment: Number(increment) | ||
} | ||
} | ||
} |
Oops, something went wrong.