Snowflake
function
to_snowflake(snowflake)
¶
Helper function to convert something into correct Discord snowflake int, gives more helpful errors Use internally to sanitize user input or in user- facing APIs (a must).
For Discord-API - facing code, just int() is sufficient
Source code in naff/models/discord/snowflake.py
def to_snowflake(snowflake: Snowflake_Type) -> int:
"""
Helper function to convert something into correct Discord snowflake int, gives more helpful errors Use internally to sanitize user input or in user- facing APIs (a must).
For Discord-API - facing code, just int() is sufficient
"""
try:
snowflake = int(snowflake)
except TypeError as e:
raise TypeError(
f"ID (snowflake) should be instance of int, str, SnowflakeObject, or support __int__. "
f"Got '{snowflake}' ({type(snowflake)}) instead."
) from e
except ValueError as e:
raise ValueError(f"ID (snowflake) should represent int. Got '{snowflake}' ({type(snowflake)}) instead.") from e
if 22 > snowflake.bit_length() > 64:
raise ValueError(
f"ID (snowflake) is not in correct Discord format! Bit length of int should be from 22 to 64 "
f"Got '{snowflake}' (bit length {snowflake.bit_length()})"
)
return snowflake
attrs
class
SnowflakeObject
¶
Attr attributes:
Name | Type | Description |
---|---|---|
id |
int |
The custom emoji id. Leave empty if you are using standard unicode emoji. |
Source code in naff/models/discord/snowflake.py
@define(slots=False)
class SnowflakeObject:
id: int = field(repr=True, converter=to_snowflake, metadata={"docs": "Discord unique snowflake ID"})
def __eq__(self, other: "SnowflakeObject") -> bool:
if hasattr(other, "id"):
other = other.id
return self.id == other
def __ne__(self, other: "SnowflakeObject") -> bool:
return self.id != other.id
def __hash__(self) -> int:
return self.id << 32
def __int__(self) -> int:
return self.id
@property
def created_at(self) -> "models.Timestamp":
"""
Returns a timestamp representing the date-time this discord object was created.
:Returns:
"""
return models.Timestamp.from_snowflake(self.id)
property
readonly
created_at: models.Timestamp
¶
Returns a timestamp representing the date-time this discord object was created.
:Returns: