feat(GuildMember): new kick() method

This commit is contained in:
grey-cat-1908 2022-05-28 12:19:18 +03:00
parent b70bb6082c
commit 702f7d3129
4 changed files with 137 additions and 52 deletions

View file

@ -5,7 +5,7 @@ from .client import Client, Bot
from .models import * from .models import *
from .exceptions import * from .exceptions import *
from .rest import RESTApp from .rest import RESTApp
from .cache import (CacheManager, AutoCacheModels) from .cache import CacheManager, AutoCacheModels
__package__ = "melisa" __package__ = "melisa"
__title__ = "Melisa" __title__ = "Melisa"

View file

@ -122,8 +122,12 @@ class GuildMember(APIModelBase):
return self return self
async def timeout(self, *, duration: Optional[float] = None, async def timeout(
until: Optional[datetime.datetime] = None): self,
*,
duration: Optional[float] = None,
until: Optional[datetime.datetime] = None,
):
"""|coro| """|coro|
Times out the member from the guild; Times out the member from the guild;
@ -137,23 +141,64 @@ class GuildMember(APIModelBase):
until: Optional[:class:`datetime.datetime`] until: Optional[:class:`datetime.datetime`]
The expiry date/time of the member's timeout. Set to ``None`` to remove the timeout. The expiry date/time of the member's timeout. Set to ``None`` to remove the timeout.
Supports up to 28 days in the future. 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: if duration is None and until is None:
await self._client.rest.modify_guild_member( await self._client.rest.modify_guild_member(
self.guild_id, self.guild_id, self.user.id, communication_disabled_until=None
self.user.id,
communication_disabled_until=None
) )
elif duration is not None: elif duration is not None:
await self._client.rest.modify_guild_member( await self._client.rest.modify_guild_member(
self.guild_id, self.guild_id,
self.user.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: else:
await self._client.rest.modify_guild_member( await self._client.rest.modify_guild_member(
self.guild_id, self.guild_id, self.user.id, communication_disabled_until=until
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
)

View file

@ -96,11 +96,11 @@ class RESTApp:
return _choose_channel_type(data) return _choose_channel_type(data)
async def delete_message( async def delete_message(
self, self,
channel_id: Union[Snowflake, str, int], channel_id: Union[Snowflake, str, int],
message_id: Union[Snowflake, str, int], message_id: Union[Snowflake, str, int],
*, *,
reason: Optional[str] = None, reason: Optional[str] = None,
): ):
"""|coro| """|coro|
@ -129,18 +129,18 @@ class RESTApp:
) )
async def create_message( async def create_message(
self, self,
channel_id: Union[Snowflake, str, int], channel_id: Union[Snowflake, str, int],
content: str = None, content: str = None,
*, *,
tts: bool = False, tts: bool = False,
embed: Embed = None, embed: Embed = None,
embeds: List[Embed] = None, embeds: List[Embed] = None,
file: File = None, file: File = None,
files: List[File] = None, files: List[File] = None,
allowed_mentions: AllowedMentions = None, allowed_mentions: AllowedMentions = None,
delete_after: int = None, delete_after: int = None,
_client_allowed_mentions: AllowedMentions = None, _client_allowed_mentions: AllowedMentions = None,
) -> Message: ) -> Message:
"""|coro| """|coro|
@ -224,13 +224,13 @@ class RESTApp:
return message_data return message_data
async def get_channel_messages_history( async def get_channel_messages_history(
self, self,
channel_id: Union[Snowflake, str, int], channel_id: Union[Snowflake, str, int],
limit: int = 50, limit: int = 50,
*, *,
before: Optional[Snowflake] = None, before: Optional[Snowflake] = None,
after: Optional[Snowflake] = None, after: Optional[Snowflake] = None,
around: Optional[Snowflake] = None, around: Optional[Snowflake] = None,
) -> AsyncIterator[Message]: ) -> AsyncIterator[Message]:
"""|coro| """|coro|
@ -301,9 +301,9 @@ class RESTApp:
limit -= search_limit limit -= search_limit
async def fetch_message( async def fetch_message(
self, self,
channel_id: Union[Snowflake, int, str], channel_id: Union[Snowflake, int, str],
message_id: Union[Snowflake, int, str], message_id: Union[Snowflake, int, str],
) -> Message: ) -> Message:
"""|coro| """|coro|
@ -338,7 +338,7 @@ class RESTApp:
return Message.from_dict(message) return Message.from_dict(message)
async def fetch_channel_pins( async def fetch_channel_pins(
self, channel_id: Union[Snowflake, int, str] self, channel_id: Union[Snowflake, int, str]
) -> AsyncIterator[Message]: ) -> AsyncIterator[Message]:
"""|coro| """|coro|
@ -368,17 +368,17 @@ class RESTApp:
yield Message.from_dict(message) yield Message.from_dict(message)
async def modify_guild_member( async def modify_guild_member(
self, self,
guild_id: Union[Snowflake, str, int], guild_id: Union[Snowflake, str, int],
user_id: Union[Snowflake, str, int], user_id: Union[Snowflake, str, int],
*, *,
nick: Optional[str] = UNDEFINED, nick: Optional[str] = UNDEFINED,
roles: Optional[List[Snowflake]] = UNDEFINED, roles: Optional[List[Snowflake]] = UNDEFINED,
is_mute: Optional[bool] = UNDEFINED, is_mute: Optional[bool] = UNDEFINED,
is_deaf: Optional[bool] = UNDEFINED, is_deaf: Optional[bool] = UNDEFINED,
voice_channel_id: Optional[Snowflake] = UNDEFINED, voice_channel_id: Optional[Snowflake] = UNDEFINED,
communication_disabled_until: Optional[datetime.datetime] = UNDEFINED, communication_disabled_until: Optional[datetime.datetime] = UNDEFINED,
reason: Optional[str] = None, reason: Optional[str] = None,
): ):
"""|coro| """|coro|
@ -420,7 +420,7 @@ class RESTApp:
**Required permissions:** ``MODERATE_MEMBERS`` **Required permissions:** ``MODERATE_MEMBERS``
reason: Optional[:class:`str`] reason: Optional[:class:`str`]
The reason of the message delete operation. The reason of the action.
Raises Raises
------- -------
@ -446,10 +446,49 @@ class RESTApp:
if voice_channel_id is not UNDEFINED: if voice_channel_id is not UNDEFINED:
data["channel_id"] = voice_channel_id data["channel_id"] = voice_channel_id
if communication_disabled_until is not UNDEFINED: 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( await self._http.patch(
f"guilds/{guild_id}/members/{user_id}", f"guilds/{guild_id}/members/{user_id}",
data=data, data=data,
headers={"X-Audit-Log-Reason": reason}, 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},
)

View file

@ -1,2 +1,3 @@
flake8==4.0.1 flake8==4.0.1
pytest==7.1.2 pytest==7.1.2
black==22.3.0