diff --git a/melisa/client.py b/melisa/client.py index da2d41d..a83d2d9 100644 --- a/melisa/client.py +++ b/melisa/client.py @@ -75,7 +75,7 @@ class Client: self.shards: Dict[int, Shard] = {} self.rest: RESTApp = RESTApp(token) - self.http = self.rest.http + self.http = self.rest._http self._events: Dict[str, Coro] = {} self._waiter_mgr = WaiterMgr(self._loop) diff --git a/melisa/models/guild/channel.py b/melisa/models/guild/channel.py index c899a06..a917a79 100644 --- a/melisa/models/guild/channel.py +++ b/melisa/models/guild/channel.py @@ -485,7 +485,7 @@ class MessageableChannel(Channel): ) async def delete_message( - self, message_id: Optional[Snowflake, str, int], *, reason: Optional[str] = None + self, message_id: Union[Snowflake, str, int], *, reason: Optional[str] = None ): """|coro| @@ -506,20 +506,21 @@ class MessageableChannel(Channel): You do not have proper permissions to do the actions required. (You must have ``MANAGE_MESSAGES`` permission) """ - await self._http.delete( - f"channels/{self.id}/messages/{message_id}", - headers={"X-Audit-Log-Reason": reason}, + await self._client.rest.delete_message( + self.id, message_id, reason=reason ) async def send( self, 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 ) -> Message: """|coro| @@ -531,6 +532,8 @@ class MessageableChannel(Channel): ---------- content: Optional[:class:`str`] The content of the message to send. + tts: Optional[:class:`bool`] + Whether the message should be sent using text-to-speech. embed: Optional[:class:`~melisa.models.message.embed.Embed`] Embed embeds: Optional[List[:class:`~melisa.models.message.embed.Embed`]] @@ -539,6 +542,12 @@ class MessageableChannel(Channel): File files: Optional[List[:class:`~melisa.models.message.file.File`]] List of files + allowed_mentions: Optional[:class:`~melisa.models.message.message.AllowedMentions`] + Controls the mentions being processed in this message. + delete_after: Optional[:class:`int`] + Provided value must be an int. + if provided, deletes message after some seconds. + May raise ``ForbiddenError`` or ``NotFoundError``. Raises ------- @@ -567,6 +576,7 @@ class MessageableChannel(Channel): ) payload["embeds"] = embeds + payload["tts"] = tts # ToDo: add auto allowed_mentions from client if allowed_mentions is not None: @@ -574,7 +584,7 @@ class MessageableChannel(Channel): content_type, data = create_form(payload, files) - return Message.from_dict( + message_data = Message.from_dict( await self._http.post( f"/channels/{self.id}/messages", data=data, @@ -582,6 +592,11 @@ class MessageableChannel(Channel): ) ) + if delete_after: + await message_data.delete(delay=delete_after) + + return message_data + async def purge( self, limit: int = 50, diff --git a/melisa/models/message/message.py b/melisa/models/message/message.py index 86cc3ff..3997724 100644 --- a/melisa/models/message/message.py +++ b/melisa/models/message/message.py @@ -3,6 +3,7 @@ from __future__ import annotations +import asyncio from dataclasses import dataclass from enum import IntEnum from typing import List, TYPE_CHECKING, Optional, Dict, Any, Union @@ -405,3 +406,33 @@ class Message(APIModelBase): f"channels/{self.channel_id}/pins/{self.id}", headers={"X-Audit-Log-Reason": reason}, ) + + async def delete(self, *, delay: Optional[float] = None) -> None: + """|coro| + + Deletes the message. + + Parameters + ---------- + delay: Optional[:class:`float`] + If provided, the number of seconds to wait in the background + before deleting the message. + + Raises + ------ + Forbidden + You do not have proper permissions to delete the message. + NotFound + The message was deleted already + HTTPException + Deleting the message failed. + """ + + if delay is not None: + async def delete(delete_after: float): + await asyncio.sleep(delete_after) + await self._client.rest.delete_message(self.channel_id, self.id) + + asyncio.create_task(delete(delay)) + else: + await self._client.rest.delete_message(self.channel_id, self.id) diff --git a/melisa/rest.py b/melisa/rest.py index df741f3..f03fed2 100644 --- a/melisa/rest.py +++ b/melisa/rest.py @@ -1,7 +1,7 @@ # Copyright MelisaDev 2022 - Present # Full MIT License can be found in `LICENSE.txt` at the project root. -from typing import Union +from typing import Union, Optional from .core.http import HTTPClient from .utils.snowflake import Snowflake @@ -23,11 +23,11 @@ class RESTApp: """ def __init__(self, token: str): - self.http: HTTPClient = HTTPClient(token) + self._http: HTTPClient = HTTPClient(token) async def fetch_user(self, user_id: Union[Snowflake, int, str]) -> User: """ - Fetch User from the Discord API (by id). + [**REST API**] Fetch User from the Discord API (by id). Parameters ---------- @@ -35,13 +35,13 @@ class RESTApp: Id of user to fetch """ - data = await self.http.get(f"users/{user_id}") + data = await self._http.get(f"users/{user_id}") return User.from_dict(data) async def fetch_guild(self, guild_id: Union[Snowflake, int, str]) -> Guild: """ - Fetch Guild from the Discord API (by id). + [**REST API**] Fetch Guild from the Discord API (by id). Parameters ---------- @@ -49,13 +49,13 @@ class RESTApp: Id of guild to fetch """ - data = await self.http.get(f"guilds/{guild_id}") + data = await self._http.get(f"guilds/{guild_id}") return Guild.from_dict(data) async def fetch_channel(self, channel_id: Union[Snowflake, str, int]) -> Channel: """ - Fetch Channel from the Discord API (by id). + [**REST API**] Fetch Channel from the Discord API (by id). Parameters ---------- @@ -63,6 +63,39 @@ class RESTApp: Id of channel to fetch """ - data = await self.http.get(f"channels/{channel_id}") + data = await self._http.get(f"channels/{channel_id}") 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 + ): + """|coro| + + [**REST API**] Deletes only one specified message. + + Parameters + ---------- + channel_id: Union[:class:`int`, :class:`str`, :class:`~.melisa.utils.snowflake.Snowflake`] + Id of channel, where message should be deleted + message_id: Union[:class:`int`, :class:`str`, :class:`~.melisa.utils.snowflake.Snowflake`] + Id of message to delete. + reason: Optional[:class:`str`] + The reason of the message delete operation. + + 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. + (You must have ``MANAGE_MESSAGES`` permission) + """ + await self._http.delete( + f"channels/{channel_id}/messages/{message_id}", + headers={"X-Audit-Log-Reason": reason}, + )