fix: guild created_at

This commit is contained in:
Victor Kotlin 2022-06-12 22:11:12 +03:00
parent 73496d0a1e
commit 53acd07401
4 changed files with 37 additions and 38 deletions

View file

@ -35,9 +35,9 @@ class CacheManager:
{} if auto_unused_attributes is not None else auto_unused_attributes {} if auto_unused_attributes is not None else auto_unused_attributes
) )
self._raw_guilds: Dict[Snowflake, Any] = {} self._raw_guilds: Dict[str, Any] = {}
self._raw_users: Dict[Snowflake, Any] = {} self._raw_users: Dict[str, Any] = {}
self._raw_dm_channels: Dict[Snowflake, Any] = {} self._raw_dm_channels: Dict[str, Any] = {}
self._disabled = disabled self._disabled = disabled
@ -51,7 +51,7 @@ class CacheManager:
# like we save channel in Guild and save it here # like we save channel in Guild and save it here
# and if you need channel, and you don't know its guild # and if you need channel, and you don't know its guild
# you can use special method, and it will find it in 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: def guilds_count(self) -> int:
"""Cached Guilds Count""" """Cached Guilds Count"""
@ -116,16 +116,16 @@ class CacheManager:
) )
for sym in channels: 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: if self._channel_symlinks.get(sym_id, UNDEFINED) is not UNDEFINED:
self._channel_symlinks.pop(sym_id) self._channel_symlinks.pop(sym_id)
self._channel_symlinks[sym_id] = guild.id self._channel_symlinks[sym_id] = str(guild.id)
else: else:
if hasattr(guild, "channels"): if hasattr(guild, "channels"):
guild.channels = {} guild.channels = {}
self._raw_guilds.update({guild.id: guild}) self._raw_guilds.update({str(guild.id): guild})
return guild return guild
@ -149,15 +149,15 @@ class CacheManager:
channel, Channel channel, Channel
) # ToDo: add channel type ) # 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 guild != UNDEFINED:
if hasattr(guild, "channels"): 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 return channel
@ -174,7 +174,7 @@ class CacheManager:
if self._disabled: if self._disabled:
return None return None
channel_id = Snowflake(int(channel_id)) channel_id = str(channel_id)
guild_id = self._channel_symlinks.get(channel_id, UNDEFINED) guild_id = self._channel_symlinks.get(channel_id, UNDEFINED)
if guild_id == UNDEFINED: if guild_id == UNDEFINED:
@ -203,8 +203,8 @@ class CacheManager:
if self._disabled: if self._disabled:
return None return None
if not isinstance(guild_id, Snowflake): if not isinstance(guild_id, str):
guild_id = Snowflake(int(guild_id)) guild_id = str(guild_id)
return self._raw_guilds.get(guild_id, None) return self._raw_guilds.get(guild_id, None)
def _set_none_guilds(self, guilds: List[Dict[str, Any]]) -> None: def _set_none_guilds(self, guilds: List[Dict[str, Any]]) -> None:
@ -222,7 +222,7 @@ class CacheManager:
guilds_dict = dict( guilds_dict = dict(
map( map(
lambda i: (Snowflake(int(i["id"])), UnavailableGuild.from_dict(i)), lambda i: (str(i["id"]), UnavailableGuild.from_dict(i)),
guilds, guilds,
) )
) )
@ -243,8 +243,8 @@ class CacheManager:
if self._disabled: if self._disabled:
return return
if not isinstance(guild_id, Snowflake): if not isinstance(guild_id, str):
guild_id = Snowflake(int(guild_id)) guild_id = str(guild_id)
return self._raw_guilds.pop(guild_id, None) return self._raw_guilds.pop(guild_id, None)
@ -261,10 +261,10 @@ class CacheManager:
if self._disabled: if self._disabled:
return return
if not isinstance(channel_id, Snowflake): if not isinstance(channel_id, str):
channel_id = Snowflake(int(channel_id)) 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: if guild_id is None:
return None return None
@ -297,11 +297,11 @@ class CacheManager:
message_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`] message_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`]
ID of message to set. ID of message to set.
""" """
if not isinstance(channel_id, Snowflake): if not isinstance(channel_id, str):
channel_id = Snowflake(int(channel_id)) channel_id = str(channel_id)
if not isinstance(guild_id, Snowflake): if not isinstance(guild_id, str):
guild_id = Snowflake(int(guild_id)) guild_id = str(guild_id)
if not isinstance(message_id, Snowflake): if not isinstance(message_id, Snowflake):
message_id = Snowflake(int(message_id)) message_id = Snowflake(int(message_id))

View file

@ -86,9 +86,9 @@ class Gateway:
"properties": { "properties": {
"$os": sys.platform, "$os": sys.platform,
"$browser": "Discord iOS" "$browser": "Discord iOS"
if kwargs.get("mobile") is not None if kwargs.get("mobile") is True
else "MelisaPy", else "melisa",
"$device": "Melisa Python Library", "$device": "melisa",
}, },
"compress": True, "compress": True,
"shard": [shard_id, num_shards], "shard": [shard_id, num_shards],

View file

@ -4,6 +4,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timezone
from enum import IntEnum from enum import IntEnum
from typing import List, Any, Optional, overload, Dict, TYPE_CHECKING, Union from typing import List, Any, Optional, overload, Dict, TYPE_CHECKING, Union
@ -239,8 +240,6 @@ class Guild(APIModelBase):
System channel flags System channel flags
rules_channel_id: APINullable[:class:`~melisa.utils.types.Snowflake`] rules_channel_id: APINullable[:class:`~melisa.utils.types.Snowflake`]
The id of the channel where Community guilds can display rules and/or guidelines 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`] large: APINullable[:class:`bool`]
True if this is considered a large guild True if this is considered a large guild
unavailable: :class:`bool` unavailable: :class:`bool`
@ -335,7 +334,6 @@ class Guild(APIModelBase):
system_channel_id: APINullable[Snowflake] = None system_channel_id: APINullable[Snowflake] = None
system_channel_flags: APINullable[SystemChannelFlags] = None system_channel_flags: APINullable[SystemChannelFlags] = None
rules_channel_id: APINullable[Snowflake] = None rules_channel_id: APINullable[Snowflake] = None
joined_at: APINullable[Timestamp] = None
large: APINullable[bool] = None large: APINullable[bool] = None
unavailable: APINullable[bool] = None unavailable: APINullable[bool] = None
@ -376,7 +374,7 @@ class Guild(APIModelBase):
""" """
self: Guild = super().__new__(cls) 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.member_count = data.get("member_count", 0)
self.name = data.get("name", "") self.name = data.get("name", "")
self.region = data.get("region") self.region = data.get("region")
@ -451,11 +449,6 @@ class Guild(APIModelBase):
else: else:
self.afk_channel_id = None 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.stage_instances = data.get("stage_instances")
self.scheduled_events = data.get("guild_scheduled_events") self.scheduled_events = data.get("guild_scheduled_events")
self.owner = None if data.get("owner") is None else data["owner"] self.owner = None if data.get("owner") is None else data["owner"]
@ -485,6 +478,11 @@ class Guild(APIModelBase):
return self 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: def icon_url(self, *, size: int = 1024, image_format: str = None) -> str | None:
# ToDo: Add Docstrings # ToDo: Add Docstrings
"""Icon Url (from the Discord CDN server)""" """Icon Url (from the Discord CDN server)"""

View file

@ -19,6 +19,7 @@ class Snowflake(int):
_MAX_VALUE: int = 9223372036854775807 _MAX_VALUE: int = 9223372036854775807
_MIN_VALUE: int = 0 _MIN_VALUE: int = 0
_DISCORD_EPOCH = 1420070400
def __init__(self, _): def __init__(self, _):
super().__init__() super().__init__()
@ -46,11 +47,11 @@ class Snowflake(int):
return Snowflake(int(string)) return Snowflake(int(string))
@property @property
def timestamp(self) -> int: def timestamp(self) -> float:
""" """
Milliseconds since Discord Epoch, the first second of 2015 or 1420070400000. Milliseconds since Discord Epoch, the first second of 2015 or 1420070400000.
""" """
return self >> 22 return (self >> 22) / 1000 + self._DISCORD_EPOCH
@property @property
def worker_id(self) -> int: def worker_id(self) -> int: