mirror of
https://github.com/MelisaDev/melisa.git
synced 2024-11-11 19:07:28 +03:00
feat(rest): add CDNBuilder
This commit is contained in:
parent
433a647168
commit
06ec5d8ca4
5 changed files with 50 additions and 16 deletions
|
@ -12,5 +12,4 @@ __title__ = "Melisa"
|
||||||
__description__ = "Cache-optimized Discord microframework for Python 3"
|
__description__ = "Cache-optimized Discord microframework for Python 3"
|
||||||
__author__ = "MelisaDev"
|
__author__ = "MelisaDev"
|
||||||
__license__ = "MIT"
|
__license__ = "MIT"
|
||||||
__version__ = '0.0.1.dev2'
|
__version__ = "0.0.1.dev2"
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,8 @@ from .utils.snowflake import Snowflake
|
||||||
|
|
||||||
|
|
||||||
class ChannelsCachingPolicy(Enum):
|
class ChannelsCachingPolicy(Enum):
|
||||||
""""Channels caching policy"""
|
""" "Channels caching policy"""
|
||||||
|
|
||||||
ALL = "all"
|
ALL = "all"
|
||||||
NONE = "none"
|
NONE = "none"
|
||||||
GUILD_TEXT = 0
|
GUILD_TEXT = 0
|
||||||
|
@ -42,11 +43,7 @@ class CacheManager:
|
||||||
|
|
||||||
# Some default values
|
# Some default values
|
||||||
if policies is None:
|
if policies is None:
|
||||||
policies = {
|
policies = {"channels": [ChannelsCachingPolicy.GUILD_TEXT]}
|
||||||
"channels": [
|
|
||||||
ChannelsCachingPolicy.GUILD_TEXT
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
self._policies = policies
|
self._policies = policies
|
||||||
|
|
||||||
|
@ -113,7 +110,9 @@ class CacheManager:
|
||||||
policy = [int(x.value) for x in policy]
|
policy = [int(x.value) for x in policy]
|
||||||
|
|
||||||
channels = filter(
|
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:
|
for sym in channels:
|
||||||
|
|
|
@ -15,7 +15,7 @@ from .channel import (
|
||||||
|
|
||||||
|
|
||||||
from .member import GuildMember
|
from .member import GuildMember
|
||||||
from ... import Role
|
from .role import Role
|
||||||
from ...utils import Snowflake, Timestamp
|
from ...utils import Snowflake, Timestamp
|
||||||
from ...utils.api_model import APIModelBase
|
from ...utils.api_model import APIModelBase
|
||||||
from ...utils.conversion import try_enum
|
from ...utils.conversion import try_enum
|
||||||
|
@ -466,6 +466,7 @@ class Guild(APIModelBase):
|
||||||
self.threads = {}
|
self.threads = {}
|
||||||
self.channels = {}
|
self.channels = {}
|
||||||
self.members = {}
|
self.members = {}
|
||||||
|
self.roles = {}
|
||||||
|
|
||||||
for member in data.get("members", []):
|
for member in data.get("members", []):
|
||||||
member = GuildMember.from_dict(member)
|
member = GuildMember.from_dict(member)
|
||||||
|
@ -480,7 +481,7 @@ class Guild(APIModelBase):
|
||||||
|
|
||||||
for role in data.get("roles", []):
|
for role in data.get("roles", []):
|
||||||
role["guild_id"] = self.id
|
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
|
return self
|
||||||
|
|
||||||
|
|
|
@ -210,11 +210,10 @@ class User(APIModelBase):
|
||||||
""":class:`str`: The user's mention string. (<@id>)"""
|
""":class:`str`: The user's mention string. (<@id>)"""
|
||||||
return "<@{}>".format(self.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)"""
|
"""Avatar url (from the Discord CDN server)"""
|
||||||
return "https://cdn.discordapp.com/avatars/{}/{}.png?size=1024".format(
|
return self._client.rest.cdn.avatar_url(self.id, self.avatar, size=size, image_format=image_format)
|
||||||
self.id, self.avatar
|
|
||||||
)
|
|
||||||
|
|
||||||
async def create_dm_channel(self):
|
async def create_dm_channel(self):
|
||||||
# ToDo: Add docstrings
|
# ToDo: Add docstrings
|
||||||
|
|
|
@ -45,10 +45,18 @@ class RESTApp:
|
||||||
----------
|
----------
|
||||||
token: :class:`str`
|
token: :class:`str`
|
||||||
The token to authorize (you can found it in the developer portal)
|
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._http: HTTPClient = HTTPClient(token)
|
||||||
|
self.cdn = CDNBuilder(default_image_format)
|
||||||
|
|
||||||
async def fetch_user(self, user_id: Union[Snowflake, int, str]) -> User:
|
async def fetch_user(self, user_id: Union[Snowflake, int, str]) -> User:
|
||||||
"""|coro|
|
"""|coro|
|
||||||
|
@ -652,3 +660,31 @@ class RESTApp:
|
||||||
f"guilds/{guild_id}/members/{user_id}/roles/{role_id}",
|
f"guilds/{guild_id}/members/{user_id}/roles/{role_id}",
|
||||||
headers={"X-Audit-Log-Reason": reason},
|
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,
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue