|
1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
|
|
2
|
|
|
# Full MIT License can be found in `LICENSE` at the project root. |
|
3
|
|
|
|
|
4
|
|
|
from __future__ import annotations |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Snowflake(int): |
|
8
|
|
|
""" |
|
9
|
|
|
Discord utilizes Twitter's snowflake format for uniquely |
|
10
|
|
|
identifiable descriptors (IDs). |
|
11
|
|
|
|
|
12
|
|
|
These IDs are guaranteed to be unique across all of Discord, |
|
13
|
|
|
except in some unique scenarios in which child objects |
|
14
|
|
|
share their parent's ID. |
|
15
|
|
|
|
|
16
|
|
|
Because Snowflake IDs are up to 64 bits in size (e.g. a uint64), |
|
17
|
|
|
they are always returned as strings in the HTTP API |
|
18
|
|
|
to prevent integer overflows in some languages. |
|
19
|
|
|
""" |
|
20
|
|
|
|
|
21
|
|
|
@classmethod |
|
22
|
|
|
def __factory__(cls, string: str) -> Snowflake: |
|
23
|
|
|
return cls.from_string(string) |
|
24
|
|
|
|
|
25
|
|
|
@classmethod |
|
26
|
|
|
def from_string(cls, string: str) -> Snowflake: |
|
27
|
|
|
""" |
|
28
|
|
|
Initialize a new Snowflake from a string. |
|
29
|
|
|
|
|
30
|
|
|
:param string: |
|
31
|
|
|
The snowflake as a string. |
|
32
|
|
|
""" |
|
33
|
|
|
return Snowflake(int(string)) |
|
34
|
|
|
|
|
35
|
|
|
@property |
|
36
|
|
|
def timestamp(self) -> int: |
|
37
|
|
|
""" |
|
38
|
|
|
Milliseconds since Discord Epoch, |
|
39
|
|
|
the first second of 2015 or 14200704000000 |
|
40
|
|
|
""" |
|
41
|
|
|
return self >> 22 |
|
42
|
|
|
|
|
43
|
|
|
@property |
|
44
|
|
|
def worker_id(self) -> int: |
|
45
|
|
|
"""Internal worker ID""" |
|
46
|
|
|
return (self >> 17) % 16 |
|
47
|
|
|
|
|
48
|
|
|
@property |
|
49
|
|
|
def process_id(self) -> int: |
|
50
|
|
|
"""Internal process ID""" |
|
51
|
|
|
return (self >> 12) % 16 |
|
52
|
|
|
|
|
53
|
|
|
@property |
|
54
|
|
|
def increment(self) -> int: |
|
55
|
|
|
""" |
|
56
|
|
|
For every ID that is generated on that process, |
|
57
|
|
|
this number is incremented |
|
58
|
|
|
""" |
|
59
|
|
|
return self % 2048 |
|
60
|
|
|
|