mirror of
https://github.com/MelisaDev/melisa.git
synced 2024-11-11 19:07:28 +03:00
feat(message): add delete_after parameter (send method)
This commit is contained in:
parent
7d43f44391
commit
11fe27a624
4 changed files with 93 additions and 14 deletions
|
@ -75,7 +75,7 @@ class Client:
|
||||||
|
|
||||||
self.shards: Dict[int, Shard] = {}
|
self.shards: Dict[int, Shard] = {}
|
||||||
self.rest: RESTApp = RESTApp(token)
|
self.rest: RESTApp = RESTApp(token)
|
||||||
self.http = self.rest.http
|
self.http = self.rest._http
|
||||||
self._events: Dict[str, Coro] = {}
|
self._events: Dict[str, Coro] = {}
|
||||||
self._waiter_mgr = WaiterMgr(self._loop)
|
self._waiter_mgr = WaiterMgr(self._loop)
|
||||||
|
|
||||||
|
|
|
@ -485,7 +485,7 @@ class MessageableChannel(Channel):
|
||||||
)
|
)
|
||||||
|
|
||||||
async def delete_message(
|
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|
|
"""|coro|
|
||||||
|
|
||||||
|
@ -506,20 +506,21 @@ class MessageableChannel(Channel):
|
||||||
You do not have proper permissions to do the actions required.
|
You do not have proper permissions to do the actions required.
|
||||||
(You must have ``MANAGE_MESSAGES`` permission)
|
(You must have ``MANAGE_MESSAGES`` permission)
|
||||||
"""
|
"""
|
||||||
await self._http.delete(
|
await self._client.rest.delete_message(
|
||||||
f"channels/{self.id}/messages/{message_id}",
|
self.id, message_id, reason=reason
|
||||||
headers={"X-Audit-Log-Reason": reason},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def send(
|
async def send(
|
||||||
self,
|
self,
|
||||||
content: str = None,
|
content: str = None,
|
||||||
*,
|
*,
|
||||||
|
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
|
||||||
) -> Message:
|
) -> Message:
|
||||||
"""|coro|
|
"""|coro|
|
||||||
|
|
||||||
|
@ -531,6 +532,8 @@ class MessageableChannel(Channel):
|
||||||
----------
|
----------
|
||||||
content: Optional[:class:`str`]
|
content: Optional[:class:`str`]
|
||||||
The content of the message to send.
|
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: Optional[:class:`~melisa.models.message.embed.Embed`]
|
||||||
Embed
|
Embed
|
||||||
embeds: Optional[List[:class:`~melisa.models.message.embed.Embed`]]
|
embeds: Optional[List[:class:`~melisa.models.message.embed.Embed`]]
|
||||||
|
@ -539,6 +542,12 @@ class MessageableChannel(Channel):
|
||||||
File
|
File
|
||||||
files: Optional[List[:class:`~melisa.models.message.file.File`]]
|
files: Optional[List[:class:`~melisa.models.message.file.File`]]
|
||||||
List of files
|
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
|
Raises
|
||||||
-------
|
-------
|
||||||
|
@ -567,6 +576,7 @@ class MessageableChannel(Channel):
|
||||||
)
|
)
|
||||||
|
|
||||||
payload["embeds"] = embeds
|
payload["embeds"] = embeds
|
||||||
|
payload["tts"] = tts
|
||||||
|
|
||||||
# ToDo: add auto allowed_mentions from client
|
# ToDo: add auto allowed_mentions from client
|
||||||
if allowed_mentions is not None:
|
if allowed_mentions is not None:
|
||||||
|
@ -574,7 +584,7 @@ class MessageableChannel(Channel):
|
||||||
|
|
||||||
content_type, data = create_form(payload, files)
|
content_type, data = create_form(payload, files)
|
||||||
|
|
||||||
return Message.from_dict(
|
message_data = Message.from_dict(
|
||||||
await self._http.post(
|
await self._http.post(
|
||||||
f"/channels/{self.id}/messages",
|
f"/channels/{self.id}/messages",
|
||||||
data=data,
|
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(
|
async def purge(
|
||||||
self,
|
self,
|
||||||
limit: int = 50,
|
limit: int = 50,
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from typing import List, TYPE_CHECKING, Optional, Dict, Any, Union
|
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}",
|
f"channels/{self.channel_id}/pins/{self.id}",
|
||||||
headers={"X-Audit-Log-Reason": reason},
|
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)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Copyright MelisaDev 2022 - Present
|
# Copyright MelisaDev 2022 - Present
|
||||||
# Full MIT License can be found in `LICENSE.txt` at the project root.
|
# 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 .core.http import HTTPClient
|
||||||
from .utils.snowflake import Snowflake
|
from .utils.snowflake import Snowflake
|
||||||
|
@ -23,11 +23,11 @@ class RESTApp:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, token: str):
|
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:
|
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
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
@ -35,13 +35,13 @@ class RESTApp:
|
||||||
Id of user to fetch
|
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)
|
return User.from_dict(data)
|
||||||
|
|
||||||
async def fetch_guild(self, guild_id: Union[Snowflake, int, str]) -> Guild:
|
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
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
@ -49,13 +49,13 @@ class RESTApp:
|
||||||
Id of guild to fetch
|
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)
|
return Guild.from_dict(data)
|
||||||
|
|
||||||
async def fetch_channel(self, channel_id: Union[Snowflake, str, int]) -> Channel:
|
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
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
@ -63,6 +63,39 @@ class RESTApp:
|
||||||
Id of channel to fetch
|
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)
|
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},
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue