diff --git a/melisa/cache.py b/melisa/cache.py index b79cd1a..74db9ef 100644 --- a/melisa/cache.py +++ b/melisa/cache.py @@ -35,9 +35,9 @@ class CacheManager: {} if auto_unused_attributes is not None else auto_unused_attributes ) - self._raw_guilds: Dict[Snowflake, Any] = {} - self._raw_users: Dict[Snowflake, Any] = {} - self._raw_dm_channels: Dict[Snowflake, Any] = {} + self._raw_guilds: Dict[str, Any] = {} + self._raw_users: Dict[str, Any] = {} + self._raw_dm_channels: Dict[str, Any] = {} self._disabled = disabled @@ -51,7 +51,7 @@ class CacheManager: # like we save channel in Guild and save it here # and if you need channel, and you don't know its guild # you can use special method, and it will find it in guild - self._channel_symlinks: Dict[Snowflake, Snowflake] = {} + self._channel_symlinks: Dict[str, str] = {} def guilds_count(self) -> int: """Cached Guilds Count""" @@ -116,16 +116,16 @@ class CacheManager: ) for sym in channels: - sym_id = Snowflake(int(sym.id)) + sym_id = str(sym.id) if self._channel_symlinks.get(sym_id, UNDEFINED) is not UNDEFINED: self._channel_symlinks.pop(sym_id) - self._channel_symlinks[sym_id] = guild.id + self._channel_symlinks[sym_id] = str(guild.id) else: if hasattr(guild, "channels"): guild.channels = {} - self._raw_guilds.update({guild.id: guild}) + self._raw_guilds.update({str(guild.id): guild}) return guild @@ -149,15 +149,15 @@ class CacheManager: channel, Channel ) # ToDo: add channel type - guild = self._raw_guilds.get(channel.guild_id, UNDEFINED) + guild = self._raw_guilds.get(str(channel.guild_id), UNDEFINED) - channel_id = Snowflake(int(channel.id)) + channel_id = str(channel.id) if guild != UNDEFINED: if hasattr(guild, "channels"): - self._raw_guilds[guild.id].channels.update({channel_id: channel}) + self._raw_guilds[str(guild.id)].channels.update({channel_id: channel}) - self._channel_symlinks.update({channel_id: channel.guild_id}) + self._channel_symlinks.update({channel_id: str(channel.guild_id)}) return channel @@ -174,7 +174,7 @@ class CacheManager: if self._disabled: return None - channel_id = Snowflake(int(channel_id)) + channel_id = str(channel_id) guild_id = self._channel_symlinks.get(channel_id, UNDEFINED) if guild_id == UNDEFINED: @@ -203,8 +203,8 @@ class CacheManager: if self._disabled: return None - if not isinstance(guild_id, Snowflake): - guild_id = Snowflake(int(guild_id)) + if not isinstance(guild_id, str): + guild_id = str(guild_id) return self._raw_guilds.get(guild_id, None) def _set_none_guilds(self, guilds: List[Dict[str, Any]]) -> None: @@ -222,7 +222,7 @@ class CacheManager: guilds_dict = dict( map( - lambda i: (Snowflake(int(i["id"])), UnavailableGuild.from_dict(i)), + lambda i: (str(i["id"]), UnavailableGuild.from_dict(i)), guilds, ) ) @@ -243,8 +243,8 @@ class CacheManager: if self._disabled: return - if not isinstance(guild_id, Snowflake): - guild_id = Snowflake(int(guild_id)) + if not isinstance(guild_id, str): + guild_id = str(guild_id) return self._raw_guilds.pop(guild_id, None) @@ -261,10 +261,10 @@ class CacheManager: if self._disabled: return - if not isinstance(channel_id, Snowflake): - channel_id = Snowflake(int(channel_id)) + if not isinstance(channel_id, str): + channel_id = str(channel_id) - guild_id = self._channel_symlinks.pop(channel_id, None) + guild_id = str(self._channel_symlinks.pop(channel_id, None)) if guild_id is None: return None @@ -297,11 +297,11 @@ class CacheManager: message_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`] ID of message to set. """ - if not isinstance(channel_id, Snowflake): - channel_id = Snowflake(int(channel_id)) + if not isinstance(channel_id, str): + channel_id = str(channel_id) - if not isinstance(guild_id, Snowflake): - guild_id = Snowflake(int(guild_id)) + if not isinstance(guild_id, str): + guild_id = str(guild_id) if not isinstance(message_id, Snowflake): message_id = Snowflake(int(message_id)) diff --git a/melisa/core/gateway.py b/melisa/core/gateway.py index a13ce27..5758291 100644 --- a/melisa/core/gateway.py +++ b/melisa/core/gateway.py @@ -86,9 +86,9 @@ class Gateway: "properties": { "$os": sys.platform, "$browser": "Discord iOS" - if kwargs.get("mobile") is not None - else "MelisaPy", - "$device": "Melisa Python Library", + if kwargs.get("mobile") is True + else "melisa", + "$device": "melisa", }, "compress": True, "shard": [shard_id, num_shards], diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index f0af6c3..6b36de3 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass +from datetime import datetime, timezone from enum import IntEnum from typing import List, Any, Optional, overload, Dict, TYPE_CHECKING, Union @@ -239,8 +240,6 @@ class Guild(APIModelBase): System channel flags rules_channel_id: APINullable[:class:`~melisa.utils.types.Snowflake`] The id of the channel where Community guilds can display rules and/or guidelines - joined_at: APINullable[:class:`~melisa.utils.Timestamp`] - When this guild was joined at large: APINullable[:class:`bool`] True if this is considered a large guild unavailable: :class:`bool` @@ -335,7 +334,6 @@ class Guild(APIModelBase): system_channel_id: APINullable[Snowflake] = None system_channel_flags: APINullable[SystemChannelFlags] = None rules_channel_id: APINullable[Snowflake] = None - joined_at: APINullable[Timestamp] = None large: APINullable[bool] = None unavailable: APINullable[bool] = None @@ -376,7 +374,7 @@ class Guild(APIModelBase): """ self: Guild = super().__new__(cls) - self.id = int(data["id"]) + self.id = Snowflake(int(data.get("id", 0))) self.member_count = data.get("member_count", 0) self.name = data.get("name", "") self.region = data.get("region") @@ -451,11 +449,6 @@ class Guild(APIModelBase): else: self.afk_channel_id = None - if data.get("joined_at") is not None: - self.joined_at = Timestamp.parse(data["joined_at"]) - else: - self.joined_at = None - self.stage_instances = data.get("stage_instances") self.scheduled_events = data.get("guild_scheduled_events") self.owner = None if data.get("owner") is None else data["owner"] @@ -485,6 +478,11 @@ class Guild(APIModelBase): return self + @property + def created_at(self): + """:class:`datetime.datetime`: Returns the guild's creation time in UTC.""" + return datetime.fromtimestamp(self.id.timestamp, tz=timezone.utc) + def icon_url(self, *, size: int = 1024, image_format: str = None) -> str | None: # ToDo: Add Docstrings """Icon Url (from the Discord CDN server)""" diff --git a/melisa/utils/snowflake.py b/melisa/utils/snowflake.py index 6a83639..9ccc833 100644 --- a/melisa/utils/snowflake.py +++ b/melisa/utils/snowflake.py @@ -19,6 +19,7 @@ class Snowflake(int): _MAX_VALUE: int = 9223372036854775807 _MIN_VALUE: int = 0 + _DISCORD_EPOCH = 1420070400 def __init__(self, _): super().__init__() @@ -46,11 +47,11 @@ class Snowflake(int): return Snowflake(int(string)) @property - def timestamp(self) -> int: + def timestamp(self) -> float: """ Milliseconds since Discord Epoch, the first second of 2015 or 1420070400000. """ - return self >> 22 + return (self >> 22) / 1000 + self._DISCORD_EPOCH @property def worker_id(self) -> int: