Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utils function for snowflake worker and process ids, with increment #770

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 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 @@ -438,6 +441,55 @@ 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

Shell1010 marked this conversation as resolved.
Show resolved Hide resolved
.. versionadded:: 2.1
Parameters
-----------
id: :class:`int`
The snowflake ID.
Shell1010 marked this conversation as resolved.
Show resolved Hide resolved

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