Skip to content

Commit 15beccb

Browse files
authored
Merge pull request #336 from JDJGInc/master
Implement discord.utils.generate_snowflake
2 parents dd37fff + de41382 commit 15beccb

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

discord/object.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,18 @@ def __repr__(self) -> str:
9191
def created_at(self) -> datetime.datetime:
9292
""":class:`datetime.datetime`: Returns the snowflake's creation time in UTC."""
9393
return utils.snowflake_time(self.id)
94+
95+
@property
96+
def worker_id(self) -> int:
97+
""":class:`int`: Returns the worker id that made the snowflake."""
98+
return (self.id & 0x3E0000) >> 17
99+
100+
@property
101+
def process_id(self) -> int:
102+
""":class:`int`: Returns the process id that made the snowflake."""
103+
return (self.id & 0x1F000) >> 12
104+
105+
@property
106+
def increment_id(self) -> int:
107+
""":class:`int`: Returns the increment id that made the snowflake."""
108+
return (self.id & 0xFFF)

discord/utils.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,19 @@
7474

7575

7676
__all__ = (
77-
'oauth_url',
78-
'snowflake_time',
79-
'time_snowflake',
80-
'find',
81-
'get',
82-
'sleep_until',
83-
'utcnow',
84-
'remove_markdown',
85-
'escape_markdown',
86-
'escape_mentions',
87-
'as_chunks',
88-
'format_dt',
77+
"oauth_url",
78+
"snowflake_time",
79+
"time_snowflake",
80+
"find",
81+
"get",
82+
"sleep_until",
83+
"utcnow",
84+
"remove_markdown",
85+
"escape_markdown",
86+
"escape_mentions",
87+
"as_chunks",
88+
"format_dt",
89+
"generate_snowflake",
8990
)
9091

9192
DISCORD_EPOCH = 1420070400000
@@ -1034,6 +1035,26 @@ def format_dt(dt: datetime.datetime, /, style: Optional[TimestampStyle] = None)
10341035
return f'<t:{int(dt.timestamp())}:{style}>'
10351036

10361037

1038+
def generate_snowflake(dt: Optional[datetime.datetime] = None) -> int:
1039+
"""Returns a numeric snowflake pretending to be created at the given date but more accurate and random than time_snowflake.
1040+
If No dt is not passed, it makes one from the current time using utcnow.
1041+
1042+
Parameters
1043+
-----------
1044+
dt: :class:`datetime.datetime`
1045+
A datetime object to convert to a snowflake.
1046+
If naive, the timezone is assumed to be local time.
1047+
1048+
Returns
1049+
--------
1050+
:class:`int`
1051+
The snowflake representing the time given.
1052+
"""
1053+
1054+
dt = dt or utcnow()
1055+
return int(dt.timestamp() * 1000 - DISCORD_EPOCH) << 22 | 0x3fffff
1056+
1057+
10371058
def basic_autocomplete(values: Union[Iterable[str],
10381059
Callable[[Interaction], Union[Iterable[str], Coroutine[Iterable[str]]]],
10391060
Coroutine[Iterable[str]]]) -> Callable[[Interaction, str], Coroutine[List[str]]]:
@@ -1081,4 +1102,4 @@ async def autocomplete_callback(interaction: Interaction,
10811102
_values = await _values
10821103
return ([x for x in _values if x.lower().startswith(value.lower())])[:25]
10831104

1084-
return autocomplete_callback
1105+
return autocomplete_callback

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,10 @@ Utility Functions
11921192

11931193
.. autofunction:: discord.utils.as_chunks
11941194

1195+
.. autofunction:: discord.utils.time_snowflake
1196+
1197+
.. autofunction:: discord.utils.generate_snowflake
1198+
11951199
.. _discord-api-enums:
11961200

11971201
Enumerations

0 commit comments

Comments
 (0)