diff --git a/discord/utils.py b/discord/utils.py index 7908b5ec..6f8f5e36 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -84,6 +84,7 @@ __all__ = ( "escape_mentions", "as_chunks", "format_dt", + "generate_snowflake", ) DISCORD_EPOCH = 1420070400000 @@ -1020,3 +1021,23 @@ def format_dt(dt: datetime.datetime, /, style: Optional[TimestampStyle] = None) if style is None: return f"" return f"" + + +def generate_snowflake(dt: Optional[datetime.datetime] = None) -> int: + """Returns a numeric snowflake pretending to be created at the given date but more accurate and random than time_snowflake. + If No dt is not passed, it makes one from the current time using utcnow. + + Parameters + ----------- + dt: :class:`datetime.datetime` + A datetime object to convert to a snowflake. + If naive, the timezone is assumed to be local time. + + Returns + -------- + :class:`int` + The snowflake representing the time given. + """ + + dt = dt or utcnow() + return int(dt.timestamp() * 1000 - DISCORD_EPOCH) << 22 | 0x3fffff \ No newline at end of file diff --git a/docs/api.rst b/docs/api.rst index 5fc56af1..ca6c9777 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -1136,6 +1136,8 @@ Utility Functions .. autofunction:: discord.utils.as_chunks +.. autofunction:: discord.utils.generate_snowflake + .. _discord-api-enums: Enumerations