From 1e5c1d9e48186bf415e205e04663136b263e39ed Mon Sep 17 00:00:00 2001 From: shell1010 Date: Fri, 3 Jan 2025 23:21:21 +0000 Subject: [PATCH] Added increment, process and worker ids to utils.py (#770) --- discord/utils.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/discord/utils.py b/discord/utils.py index 911c460b3dbb..944725c3c08d 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -100,6 +100,9 @@ __all__ = ( 'oauth_url', 'snowflake_time', + 'snowflake_worker_id', + 'snowflake_process_id', + 'snowflake_increment', 'time_snowflake', 'find', 'get', @@ -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.