From bda6b2244dbb935b824f012a146ec8adda4b2a61 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Sun, 27 Mar 2022 16:31:53 +0300 Subject: [PATCH] create channel method --- melisa/core/gateway.py | 5 ++- melisa/models/guild/guild.py | 77 +++++++++++++++++++++++++++++++++++- melisa/utils/api_model.py | 3 ++ 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/melisa/core/gateway.py b/melisa/core/gateway.py index d736052..093a250 100644 --- a/melisa/core/gateway.py +++ b/melisa/core/gateway.py @@ -124,6 +124,7 @@ class Gateway: return None async def handle_data(self, data): + """Handles received data and process it""" if data["op"] == self.DISPATCH: self.sequence = int(data["s"]) event_type = data["t"].lower() @@ -142,6 +143,7 @@ class Gateway: self.latency = time.perf_counter() - self._last_send async def receive(self) -> None: + """Receives and parses received data""" async for msg in self.ws: if msg.type == aiohttp.WSMsgType.BINARY: data = await self.parse_websocket_message(msg.data) @@ -155,8 +157,7 @@ class Gateway: close_code = self.ws.close_code if close_code is None: return - else: - await self.handle_close(close_code) + await self.handle_close(close_code) async def handle_close(self, code: int) -> None: if code == 4009: diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index fd6afb4..0b8660b 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -5,8 +5,9 @@ from __future__ import annotations from dataclasses import dataclass from enum import IntEnum, Enum -from typing import List, Any +from typing import List, Any, Optional, overload +from .channel import Channel, ChannelType, channel_types_for_converting from ...utils import Snowflake from ...utils import APIModelBase from ...utils.types import APINullable @@ -429,6 +430,80 @@ class Guild(APIModelBase): # TODO: Make a structure for welcome_screen, stage_instances, # stickers and guild_scheduled_events + @overload + async def create_channel( + self, + *, + name: str, + type: Optional[ChannelType] = None, + topic: Optional[str] = None, + bitrate: Optional[int] = None, + user_limit: Optional[int] = None, + rate_limit_per_user: Optional[int] = None, + position: Optional[int] = None, + permission_overwrites: Optional[List[Any]] = None, + parent_id: Optional[Snowflake] = None, + nsfw: Optional[bool] = None, + reason: Optional[str] = None, + ) -> Channel: + """|coro| + Create a new channel object for the guild. + + Parameters + ---------- + name: str + channel name (1-100 characters) + type: Optional[:class:int`] + the type of channel + topic: Optional[:class:str`] + channel topic (0-1024 characters) + bitrate: Optional[:class:`int`] + the bitrate (in bits) of the voice channel (voice only) + user_limit: Optional[:class:`int`] + the user limit of the voice channel (voice only) + rate_limit_per_user: Optional[:class:`int`] + amount of seconds a user has to wait + before sending another message (0-21600) + bots, as well as users with the permission + manage_messages or manage_channel, are unaffected + position: Optional[:class:`int`] + sorting position of the channel + permission_overwrites: Optional[List[Any]] + the channel's permission overwrites + parent_id: Optional[:class:`~melisa.Snowflake`] + id of the parent category for a channel + nsfw: Optional[:class:`bool`] + whether the channel is nsfw + reason: Optional[:class:`str`] + audit log reason |default| :data:`None` + + Raises + ------- + BadRequestError + If some specified parameters are wrong. + ForbiddenError + You do not have proper permissions to do the actions required. + This method requires `MANAGE_CHANNELS` permission. + Setting `MANAGE_ROLES` permission in channels is only possible for guild administrators. + + Returns + ------- + :class:`~melisa.models.guild.channel.Channel` + New channel object. + """ + + async def create_channel(self, *, reason: Optional[str] = None, **kwargs): + data = await self._http.post( + f"guilds/{self.id}/channels", + data=kwargs, + headers={"X-Audit-Log-Reason": reason}, + ) + + data.update({"type": ChannelType(data.pop("type"))}) + + channel_cls = channel_types_for_converting.get(data["type"], Channel) + return channel_cls.from_dict(data) + @dataclass(repr=False) class UnavailableGuild(APIModelBase): diff --git a/melisa/utils/api_model.py b/melisa/utils/api_model.py index a3a79ad..905de30 100644 --- a/melisa/utils/api_model.py +++ b/melisa/utils/api_model.py @@ -19,6 +19,9 @@ T = TypeVar("T") def to_dict_without_none(model): + """ + Converts discord model or other object to dict. + """ if _is_dataclass_instance(model): result = []