Skip to content

Commit

Permalink
Added increment, process and worker ids to utils.py (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shell1010 authored and dolfies committed Jan 12, 2025
1 parent 19db7c9 commit 1e5c1d9
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
__all__ = (
'oauth_url',
'snowflake_time',
'snowflake_worker_id',
'snowflake_process_id',
'snowflake_increment',
'time_snowflake',
'find',
'get',
Expand Down Expand Up @@ -440,6 +443,58 @@ def oauth_url(
url += f'&{urlencode({"state": state})}'
return url

def snowflake_worker_id(id: int, /) -> int:
"""Returns the worker ID of the given snowflake
.. versionadded:: 2.1
Parameters
-----------
id: :class:`int`
The snowflake ID.
Returns
--------
:class:`int`
The worker ID used to generate the snowflake.
"""
return (id >> 17) & 0x1F

def snowflake_process_id(id: int, /) -> int:
"""Returns the process ID of the given snowflake
.. versionadded:: 2.1
Parameters
-----------
id: :class:`int`
The snowflake ID.
Returns
--------
:class:`int`
The process ID used to generate the snowflake.
"""
return (id >> 12) & 0x1F

def snowflake_increment(id: int, /) -> int:
"""Returns the increment of the given snowflake.
For every generated ID on that process, this number is incremented.
.. versionadded:: 2.1
Parameters
-----------
id: :class:`int`
The snowflake ID.
Returns
--------
:class:`int`
The increment of current snowflake.
"""
return id & 0xFFF


def snowflake_time(id: int, /) -> datetime.datetime:
"""Returns the creation time of the given snowflake.
Expand Down

0 comments on commit 1e5c1d9

Please sign in to comment.