diff --git a/melisa/__init__.py b/melisa/__init__.py index 0864f2a..2636d32 100644 --- a/melisa/__init__.py +++ b/melisa/__init__.py @@ -12,5 +12,4 @@ __title__ = "Melisa" __description__ = "Cache-optimized Discord microframework for Python 3" __author__ = "MelisaDev" __license__ = "MIT" -__version__ = '0.0.1.dev2' - +__version__ = "0.0.1.dev2" diff --git a/melisa/cache.py b/melisa/cache.py index 006f34a..b79cd1a 100644 --- a/melisa/cache.py +++ b/melisa/cache.py @@ -13,7 +13,8 @@ from .utils.snowflake import Snowflake class ChannelsCachingPolicy(Enum): - """"Channels caching policy""" + """ "Channels caching policy""" + ALL = "all" NONE = "none" GUILD_TEXT = 0 @@ -42,11 +43,7 @@ class CacheManager: # Some default values if policies is None: - policies = { - "channels": [ - ChannelsCachingPolicy.GUILD_TEXT - ] - } + policies = {"channels": [ChannelsCachingPolicy.GUILD_TEXT]} self._policies = policies @@ -113,7 +110,9 @@ class CacheManager: policy = [int(x.value) for x in policy] channels = filter( - lambda channel: channel.type is not None and int(channel.type) in policy, channels + lambda channel: channel.type is not None + and int(channel.type) in policy, + channels, ) for sym in channels: diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index 2c6f7d8..655ece0 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -15,7 +15,7 @@ from .channel import ( from .member import GuildMember -from ... import Role +from .role import Role from ...utils import Snowflake, Timestamp from ...utils.api_model import APIModelBase from ...utils.conversion import try_enum @@ -466,6 +466,7 @@ class Guild(APIModelBase): self.threads = {} self.channels = {} self.members = {} + self.roles = {} for member in data.get("members", []): member = GuildMember.from_dict(member) @@ -480,7 +481,7 @@ class Guild(APIModelBase): for role in data.get("roles", []): role["guild_id"] = self.id - self.roles[Snowflake(int(role.id))] = Role.from_dict(role) + self.roles[Snowflake(int(role["id"]))] = Role.from_dict(role) return self diff --git a/melisa/models/user/user.py b/melisa/models/user/user.py index ea0bced..26be355 100644 --- a/melisa/models/user/user.py +++ b/melisa/models/user/user.py @@ -210,11 +210,10 @@ class User(APIModelBase): """:class:`str`: The user's mention string. (<@id>)""" return "<@{}>".format(self.id) - def avatar_url(self) -> str: + def avatar_url(self, *, size: int = 1024, image_format: str = None) -> str: + # ToDo: Add Docstrings """Avatar url (from the Discord CDN server)""" - return "https://cdn.discordapp.com/avatars/{}/{}.png?size=1024".format( - self.id, self.avatar - ) + return self._client.rest.cdn.avatar_url(self.id, self.avatar, size=size, image_format=image_format) async def create_dm_channel(self): # ToDo: Add docstrings diff --git a/melisa/rest.py b/melisa/rest.py index e02471f..690ffea 100644 --- a/melisa/rest.py +++ b/melisa/rest.py @@ -45,10 +45,18 @@ class RESTApp: ---------- token: :class:`str` The token to authorize (you can found it in the developer portal) + default_image_format: :class:`str` + Default image format + + Attributes + ----------- + cdn: :class:`~melisa.rest.CDNBuilder` + CDN Builder to build images """ - def __init__(self, token: str): + def __init__(self, token: str, default_image_format: str = None): self._http: HTTPClient = HTTPClient(token) + self.cdn = CDNBuilder(default_image_format) async def fetch_user(self, user_id: Union[Snowflake, int, str]) -> User: """|coro| @@ -652,3 +660,31 @@ class RESTApp: f"guilds/{guild_id}/members/{user_id}/roles/{role_id}", headers={"X-Audit-Log-Reason": reason}, ) + + +class CDNBuilder: + """Can be used to build images + + Parameters + ---------- + default_image_format: :class:`str` + Default image format + """ + + # ToDo: Add docstrings + + BASE_URL = "https://cdn.discordapp.com" + + def __init__(self, default_image_format: str = None): + self.dif = default_image_format if default_image_format is not None else "png" + + def avatar_url( + self, user_id: str, _hash: str, *, size: int = 1024, image_format: str = None + ): + return "{}/avatars/{}/{}.{}?size={}".format( + self.BASE_URL, + user_id, + _hash, + image_format if image_format is not None else self.dif, + size, + )