Skip to content

Commit 25cb969

Browse files
committed
Added response delete/edit
1 parent 9db5566 commit 25cb969

File tree

3 files changed

+99
-7
lines changed

3 files changed

+99
-7
lines changed

discord_slash/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ def process_options(self, guild: discord.Guild, options: list, auto_convert: dic
112112
if not guild:
113113
self.logger.info("This command invoke is missing guild. Skipping option process.")
114114
return [x["value"] for x in options]
115+
if not auto_convert:
116+
return [x["value"] for x in options]
115117
converters = [guild.get_member, guild.get_role, guild.get_role]
116118
types = {
117119
"user": 0,

discord_slash/http.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,60 @@ class SlashCommandRequest:
66
def __init__(self):
77
pass
88

9-
async def post(self, _resp, _id, token):
9+
async def post(self, _resp, bot_id, interaction_id, token, initial=False) -> None:
1010
"""
1111
Sends command response POST request to Discord API.
1212
1313
:param _resp: Command response.
1414
:type _resp: dict
15-
:param _id: Command message id.
15+
:param bot_id: Bot ID.
16+
:param interaction_id: Interaction ID.
1617
:param token: Command message token.
17-
:return: True if succeeded.
18+
:param initial: Whether this request is initial. Default ``False``
19+
:return: ``None``, since Discord API doesn't return anything.
1820
:raises: :class:`.error.RequestFailure` - Requesting to API has failed.
1921
"""
20-
req_url = f"https://discord.com/api/v8/interactions/{_id}/{token}/callback"
22+
req_url = f"https://discord.com/api/v8/interactions/{interaction_id}/{token}/callback" \
23+
if initial \
24+
else f"https://discord.com/api/v8/webhooks/{bot_id}/{token}"
2125
async with aiohttp.ClientSession() as session:
2226
async with session.post(req_url, json=_resp) as resp:
27+
if not 200 <= resp.status < 300:
28+
raise RequestFailure(resp.status, await resp.text())
29+
return None
30+
31+
async def edit(self, _resp, bot_id, token, message_id="@original"):
32+
"""
33+
Sends edit command response POST request to Discord API.
34+
35+
:param _resp: Edited response.
36+
:type _resp: dict
37+
:param bot_id: Bot ID.
38+
:param token: Command message token.
39+
:param message_id: Message ID to edit. Default initial message.
40+
:return: True if succeeded.
41+
:raises: :class:`.error.RequestFailure` - Requesting to API has failed.
42+
"""
43+
req_url = f"https://discord.com/api/v8/webhooks/{bot_id}/{token}/messages/{message_id}"
44+
async with aiohttp.ClientSession() as session:
45+
async with session.patch(req_url, json=_resp) as resp:
46+
if not 200 <= resp.status < 300:
47+
raise RequestFailure(resp.status, await resp.text())
48+
return True
49+
50+
async def delete(self, bot_id, token, message_id="@original"):
51+
"""
52+
Sends delete command response POST request to Discord API.
53+
54+
:param bot_id: Bot ID.
55+
:param token: Command message token.
56+
:param message_id: Message ID to delete. Default initial message.
57+
:return: True if succeeded.
58+
:raises: :class:`.error.RequestFailure` - Requesting to API has failed.
59+
"""
60+
req_url = f"https://discord.com/api/v8/webhooks/{bot_id}/{token}/messages/{message_id}"
61+
async with aiohttp.ClientSession() as session:
62+
async with session.delete(req_url) as resp:
2363
if not 200 <= resp.status < 300:
2464
raise RequestFailure(resp.status, await resp.text())
2565
return True

discord_slash/model.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import discord
33
from discord.ext import commands
44
from . import http
5+
from . import error
56

67

78
class SlashContext:
@@ -13,6 +14,8 @@ class SlashContext:
1314
:ivar interaction_id: Interaction ID of the command message.
1415
:ivar command_id: ID of the command.
1516
:ivar _http: :class:`.http.SlashCommandRequest` of the client.
17+
:ivar _discord: :class:`discord.ext.commands.Bot`
18+
:ivar sent: Whether you sent the initial response.
1619
:ivar guild: :class:`discord.Guild` instance of the command message.
1720
:ivar author: :class:`discord.Member` instance representing author of the command message.
1821
:ivar channel: :class:`discord.TextChannel` instance representing channel of the command message.
@@ -26,6 +29,8 @@ def __init__(self,
2629
self.interaction_id = _json["id"]
2730
self.command_id = _json["data"]["id"]
2831
self._http = _http
32+
self._discord = _discord
33+
self.sent = False
2934
self.guild: discord.Guild = _discord.get_guild(int(_json["guild_id"]))
3035
self.author: discord.Member = self.guild.get_member(int(_json["member"]["user"]["id"])) if self.guild else None
3136
self.channel = self.guild.get_channel(int(_json["channel_id"])) if self.guild else None
@@ -46,10 +51,10 @@ async def send(self,
4651
:type embeds: List[discord.Embed]
4752
:param tts: Whether to speak message using tts. Default ``False``.
4853
:type tts: bool
49-
:return: `None`
54+
:return: ``None``
5055
"""
5156
if embeds and len(embeds) > 10:
52-
raise
57+
raise error.IncorrectFormat("Embed must be 10 or fewer.")
5358
base = {
5459
"type": send_type,
5560
"data": {
@@ -59,7 +64,52 @@ async def send(self,
5964
"allowed_mentions": []
6065
}
6166
}
62-
await self._http.post(base, self.interaction_id, self.__token)
67+
initial = True if not self.sent else False
68+
resp = await self._http.post(base, self._discord.user.id, self.interaction_id, self.__token, initial)
69+
self.sent = True
70+
return resp
71+
72+
async def edit(self,
73+
message_id: typing.Union[int, str] = "@original",
74+
send_type: int = 4,
75+
text: str = "",
76+
embeds: typing.List[discord.Embed] = None,
77+
tts: bool = False):
78+
"""
79+
Edits response of the slash command.
80+
81+
:param message_id: Response message ID. Default initial message.
82+
:param send_type: Type of the response. Refer Discord API DOCS for more info about types. Default ``4``.
83+
:type send_type: int
84+
:param text: Text of the response. Can be ``None``.
85+
:type text: str
86+
:param embeds: Embeds of the response. Maximum 10, can be empty.
87+
:type embeds: List[discord.Embed]
88+
:param tts: Whether to speak message using tts. Default ``False``.
89+
:type tts: bool
90+
:return: ``None``
91+
"""
92+
if embeds and len(embeds) > 10:
93+
raise error.IncorrectFormat("Embed must be 10 or fewer.")
94+
base = {
95+
"type": send_type,
96+
"data": {
97+
"tts": tts,
98+
"content": text,
99+
"embeds": [x.to_dict() for x in embeds] if embeds else [],
100+
"allowed_mentions": []
101+
}
102+
}
103+
await self._http.edit(base, self._discord.user.id, self.__token, message_id)
104+
105+
async def delete(self, message_id: typing.Union[int, str] = "@original"):
106+
"""
107+
Deletes response of the slash command.
108+
109+
:param message_id: Response message ID. Default initial message.
110+
:return: ``None``
111+
"""
112+
await self._http.delete(self._discord.user.id, self.__token, message_id)
63113

64114

65115
"""

0 commit comments

Comments
 (0)