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
|
|
|
|
39
|
|
|
@property |
40
|
|
|
def timestamp(self) -> int: |
41
|
|
|
""" |
42
|
|
|
Milliseconds since Discord Epoch, |
43
|
|
|
the first second of 2015 or 14200704000000 |
44
|
|
|
""" |
45
|
|
|
return self >> 22 |
46
|
|
|
|
47
|
|
|
@property |
48
|
|
|
def worker_id(self) -> int: |
49
|
|
|
"""Internal worker ID""" |
50
|
|
|
return (self >> 17) % 16 |
51
|
|
|
|
52
|
|
|
@property |
53
|
|
|
def process_id(self) -> int: |
54
|
|
|
"""Internal process ID""" |
55
|
|
|
return (self >> 12) % 16 |
56
|
|
|
|
57
|
|
|
@property |
58
|
|
|
def increment(self) -> int: |
59
|
|
|
""" |
60
|
|
|
For every ID that is generated on that process, |
61
|
|
|
this number is incremented |
62
|
|
|
""" |
63
|
|
|
return self % 2048 |
64
|
|
|
|