feat(rest): add CDNBuilder

This commit is contained in:
grey-cat-1908 2022-05-31 11:39:44 +03:00
parent 433a647168
commit 06ec5d8ca4
5 changed files with 50 additions and 16 deletions

View file

@ -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"

View file

@ -14,6 +14,7 @@ from .utils.snowflake import Snowflake
class ChannelsCachingPolicy(Enum):
""" "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:

View file

@ -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

View file

@ -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

View file

@ -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,
)