diff --git a/melisa/__init__.py b/melisa/__init__.py index 40c9cd5..8c4f262 100644 --- a/melisa/__init__.py +++ b/melisa/__init__.py @@ -5,7 +5,7 @@ from .client import Client, Bot from .models import * from .exceptions import * from .rest import RESTApp -from .cache import (CacheManager, AutoCacheModels) +from .cache import CacheManager, AutoCacheModels __package__ = "melisa" __title__ = "Melisa" diff --git a/melisa/models/guild/member.py b/melisa/models/guild/member.py index 590f9cd..7240654 100644 --- a/melisa/models/guild/member.py +++ b/melisa/models/guild/member.py @@ -122,8 +122,12 @@ class GuildMember(APIModelBase): return self - async def timeout(self, *, duration: Optional[float] = None, - until: Optional[datetime.datetime] = None): + async def timeout( + self, + *, + duration: Optional[float] = None, + until: Optional[datetime.datetime] = None, + ): """|coro| Times out the member from the guild; @@ -137,23 +141,64 @@ class GuildMember(APIModelBase): until: Optional[:class:`datetime.datetime`] The expiry date/time of the member's timeout. Set to ``None`` to remove the timeout. Supports up to 28 days in the future. + + Raises + ------- + HTTPException + The request to perform the action failed with other http exception. + ForbiddenError + You do not have proper permissions to do the actions required. + BadRequestError + You provided a wrong type of argument/ + + Returns + ------- + :class:`~melisa.models.guild.member.GuildMember` + This member object. """ if duration is None and until is None: await self._client.rest.modify_guild_member( - self.guild_id, - self.user.id, - communication_disabled_until=None + self.guild_id, self.user.id, communication_disabled_until=None ) elif duration is not None: await self._client.rest.modify_guild_member( self.guild_id, self.user.id, - communication_disabled_until=datetime.datetime.utcnow() + datetime.timedelta(seconds=duration) + communication_disabled_until=datetime.datetime.utcnow() + + datetime.timedelta(seconds=duration), ) else: await self._client.rest.modify_guild_member( - self.guild_id, - self.user.id, - communication_disabled_until=until + self.guild_id, self.user.id, communication_disabled_until=until ) + + return self + + async def kick( + self, + *, + reason: Optional[str] = None, + ): + """|coro| + + Kicks member from a guild. + + **Required permissions:** ``KICK_MEMBERS`` + + Parameters + ---------- + reason: Optional[:class:`str`] + The reason of the action. + + Raises + ------- + HTTPException + The request to perform the action failed with other http exception. + ForbiddenError + You do not have proper permissions to do the actions required. + """ + + await self._client.rest.remove_guild_member( + self.guild_id, self.user.id, reason=reason + ) diff --git a/melisa/rest.py b/melisa/rest.py index 9ce9bc2..475466c 100644 --- a/melisa/rest.py +++ b/melisa/rest.py @@ -96,11 +96,11 @@ class RESTApp: return _choose_channel_type(data) async def delete_message( - self, - channel_id: Union[Snowflake, str, int], - message_id: Union[Snowflake, str, int], - *, - reason: Optional[str] = None, + self, + channel_id: Union[Snowflake, str, int], + message_id: Union[Snowflake, str, int], + *, + reason: Optional[str] = None, ): """|coro| @@ -129,18 +129,18 @@ class RESTApp: ) async def create_message( - self, - channel_id: Union[Snowflake, str, int], - content: str = None, - *, - tts: bool = False, - embed: Embed = None, - embeds: List[Embed] = None, - file: File = None, - files: List[File] = None, - allowed_mentions: AllowedMentions = None, - delete_after: int = None, - _client_allowed_mentions: AllowedMentions = None, + self, + channel_id: Union[Snowflake, str, int], + content: str = None, + *, + tts: bool = False, + embed: Embed = None, + embeds: List[Embed] = None, + file: File = None, + files: List[File] = None, + allowed_mentions: AllowedMentions = None, + delete_after: int = None, + _client_allowed_mentions: AllowedMentions = None, ) -> Message: """|coro| @@ -224,13 +224,13 @@ class RESTApp: return message_data async def get_channel_messages_history( - self, - channel_id: Union[Snowflake, str, int], - limit: int = 50, - *, - before: Optional[Snowflake] = None, - after: Optional[Snowflake] = None, - around: Optional[Snowflake] = None, + self, + channel_id: Union[Snowflake, str, int], + limit: int = 50, + *, + before: Optional[Snowflake] = None, + after: Optional[Snowflake] = None, + around: Optional[Snowflake] = None, ) -> AsyncIterator[Message]: """|coro| @@ -301,9 +301,9 @@ class RESTApp: limit -= search_limit async def fetch_message( - self, - channel_id: Union[Snowflake, int, str], - message_id: Union[Snowflake, int, str], + self, + channel_id: Union[Snowflake, int, str], + message_id: Union[Snowflake, int, str], ) -> Message: """|coro| @@ -338,7 +338,7 @@ class RESTApp: return Message.from_dict(message) async def fetch_channel_pins( - self, channel_id: Union[Snowflake, int, str] + self, channel_id: Union[Snowflake, int, str] ) -> AsyncIterator[Message]: """|coro| @@ -368,17 +368,17 @@ class RESTApp: yield Message.from_dict(message) async def modify_guild_member( - self, - guild_id: Union[Snowflake, str, int], - user_id: Union[Snowflake, str, int], - *, - nick: Optional[str] = UNDEFINED, - roles: Optional[List[Snowflake]] = UNDEFINED, - is_mute: Optional[bool] = UNDEFINED, - is_deaf: Optional[bool] = UNDEFINED, - voice_channel_id: Optional[Snowflake] = UNDEFINED, - communication_disabled_until: Optional[datetime.datetime] = UNDEFINED, - reason: Optional[str] = None, + self, + guild_id: Union[Snowflake, str, int], + user_id: Union[Snowflake, str, int], + *, + nick: Optional[str] = UNDEFINED, + roles: Optional[List[Snowflake]] = UNDEFINED, + is_mute: Optional[bool] = UNDEFINED, + is_deaf: Optional[bool] = UNDEFINED, + voice_channel_id: Optional[Snowflake] = UNDEFINED, + communication_disabled_until: Optional[datetime.datetime] = UNDEFINED, + reason: Optional[str] = None, ): """|coro| @@ -420,7 +420,7 @@ class RESTApp: **Required permissions:** ``MODERATE_MEMBERS`` reason: Optional[:class:`str`] - The reason of the message delete operation. + The reason of the action. Raises ------- @@ -446,10 +446,49 @@ class RESTApp: if voice_channel_id is not UNDEFINED: data["channel_id"] = voice_channel_id if communication_disabled_until is not UNDEFINED: - data["communication_disabled_until"] = communication_disabled_until.isoformat() + data[ + "communication_disabled_until" + ] = communication_disabled_until.isoformat() await self._http.patch( f"guilds/{guild_id}/members/{user_id}", data=data, headers={"X-Audit-Log-Reason": reason}, ) + + async def remove_guild_member( + self, + guild_id: Union[Snowflake, str, int], + user_id: Union[Snowflake, str, int], + *, + reason: Optional[str] = None, + ): + """|coro| + + [**REST API**] Remove a member from a guild. + + **Required permissions:** ``KICK_MEMBERS`` + + Parameters + ---------- + guild_id: Union[:class:`int`, :class:`str`, :class:`~.melisa.utils.snowflake.Snowflake`] + Id of guild where we will remove member + user_id: Union[:class:`int`, :class:`str`, :class:`~.melisa.utils.snowflake.Snowflake`] + Id of user to operate with. + reason: Optional[:class:`str`] + The reason of the action. + + Raises + ------- + HTTPException + The request to perform the action failed with other http exception. + ForbiddenError + You do not have proper permissions to do the actions required. + BadRequestError + You provided a wrong guild, user or something else. + """ + + await self._http.delete( + f"guilds/{guild_id}/members/{user_id}", + headers={"X-Audit-Log-Reason": reason}, + ) diff --git a/packages/dev.txt b/packages/dev.txt index bc430ad..28b3c7f 100644 --- a/packages/dev.txt +++ b/packages/dev.txt @@ -1,2 +1,3 @@ flake8==4.0.1 -pytest==7.1.2 \ No newline at end of file +pytest==7.1.2 +black==22.3.0 \ No newline at end of file