|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
# MIT License |
|
3
|
|
|
# |
|
4
|
|
|
# Copyright (c) 2021 Pincer |
|
5
|
|
|
# |
|
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining |
|
7
|
|
|
# a copy of this software and associated documentation files |
|
8
|
|
|
# (the "Software"), to deal in the Software without restriction, |
|
9
|
|
|
# including without limitation the rights to use, copy, modify, merge, |
|
10
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software, |
|
11
|
|
|
# and to permit persons to whom the Software is furnished to do so, |
|
12
|
|
|
# subject to the following conditions: |
|
13
|
|
|
# |
|
14
|
|
|
# The above copyright notice and this permission notice shall be |
|
15
|
|
|
# included in all copies or substantial portions of the Software. |
|
16
|
|
|
# |
|
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
18
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
19
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
20
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
21
|
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
22
|
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
23
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
24
|
|
|
|
|
25
|
|
|
class Snowflake(int): |
|
26
|
|
|
""" |
|
27
|
|
|
Discord utilizes Twitter's snowflake format for uniquely |
|
28
|
|
|
identifiable descriptors (IDs). |
|
29
|
|
|
|
|
30
|
|
|
These IDs are guaranteed to be unique across all of Discord, |
|
31
|
|
|
except in some unique scenarios in which child objects |
|
32
|
|
|
share their parent's ID. |
|
33
|
|
|
|
|
34
|
|
|
Because Snowflake IDs are up to 64 bits in size (e.g. a uint64), |
|
35
|
|
|
they are always returned as strings in the HTTP API |
|
36
|
|
|
to prevent integer overflows in some languages. |
|
37
|
|
|
""" |
|
38
|
|
|
@classmethod |
|
39
|
|
|
def from_string(cls, string: str): |
|
40
|
|
|
""" |
|
41
|
|
|
Initialize a new Snowflake from a string. |
|
42
|
|
|
|
|
43
|
|
|
:param string: |
|
44
|
|
|
The snowflake as a string. |
|
45
|
|
|
""" |
|
46
|
|
|
return Snowflake(int(string)) |
|
47
|
|
|
|
|
48
|
|
|
@property |
|
49
|
|
|
def timestamp(self) -> int: |
|
50
|
|
|
""" |
|
51
|
|
|
Milliseconds since Discord Epoch, |
|
52
|
|
|
the first second of 2015 or 14200704000000 |
|
53
|
|
|
""" |
|
54
|
|
|
return self >> 22 |
|
55
|
|
|
|
|
56
|
|
|
@property |
|
57
|
|
|
def worker_id(self) -> int: |
|
58
|
|
|
"""Internal worker ID""" |
|
59
|
|
|
return (self >> 17) % 16 |
|
60
|
|
|
|
|
61
|
|
|
@property |
|
62
|
|
|
def process_id(self) -> int: |
|
63
|
|
|
"""Internal process ID""" |
|
64
|
|
|
return (self >> 12) % 16 |
|
65
|
|
|
|
|
66
|
|
|
@property |
|
67
|
|
|
def increment(self) -> int: |
|
68
|
|
|
""" |
|
69
|
|
|
For every ID that is generated on that process, |
|
70
|
|
|
this number is incremented |
|
71
|
|
|
""" |
|
72
|
|
|
return self % 2048 |
|
73
|
|
|
|