Skip to content

Commit b6ba21d

Browse files
committed
adds new function that is meant to create snowflake ids.
1 parent 6e2cb76 commit b6ba21d

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

discord/utils.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,19 @@
7373

7474

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

9091
DISCORD_EPOCH = 1420070400000
@@ -1027,3 +1028,23 @@ def format_dt(dt: datetime.datetime, /, style: Optional[TimestampStyle] = None)
10271028
if style is None:
10281029
return f'<t:{int(dt.timestamp())}>'
10291030
return f'<t:{int(dt.timestamp())}:{style}>'
1031+
1032+
1033+
def generate_snowflake(dt: Optional[datetime.datetime] = None) -> int:
1034+
"""Returns a numeric snowflake pretending to be created at the given date but more accurate and random than time_snowflake.
1035+
If No dt is not passed, it makes one from the current time using utcnow.
1036+
1037+
Parameters
1038+
-----------
1039+
dt: :class:`datetime.datetime`
1040+
A datetime object to convert to a snowflake.
1041+
If naive, the timezone is assumed to be local time.
1042+
1043+
Returns
1044+
--------
1045+
:class:`int`
1046+
The snowflake representing the time given.
1047+
"""
1048+
1049+
dt = dt or utcnow()
1050+
return int(dt.timestamp() * 1000 - DISCORD_EPOCH) << 22 | 0x3fffff

0 commit comments

Comments
 (0)