Skip to content

Commit 0cb880d

Browse files
Add files in edit, inital send.
1 parent 320631c commit 0cb880d

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

discord_slash/context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ async def send(self,
171171
initial_message = False
172172
if not self.responded:
173173
initial_message = True
174-
if files:
175-
raise error.IncorrectFormat("You cannot send files in the initial response!")
174+
if files and not self.deffered:
175+
await self.defer(hidden=hidden)
176176
if self.deffered:
177177
if self._deffered_hidden != hidden:
178178
self._logger.warning(
179179
"Deffered response might not be what you set it to! (hidden / visible) "
180180
"This is because it was deffered in a different state"
181181
)
182-
resp = await self._http.edit(base, self.__token)
182+
resp = await self._http.edit(base, self.__token, files = files)
183183
self.deffered = False
184184
else:
185185
json_data = {

discord_slash/http.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def post_followup(self, _resp, token, files: typing.List[discord.File] = None):
9696
:return: Coroutine
9797
"""
9898
if files:
99-
return self.post_with_files(_resp, files, token)
99+
return self.request_with_files(_resp, files, token, "POST")
100100
return self.command_response(token, True, "POST", json=_resp)
101101

102102
def post_initial_response(self, _resp, interaction_id, token):
@@ -130,27 +130,31 @@ def command_response(self, token, use_webhook, method, interaction_id= None, url
130130
route = CustomRoute(method, req_url)
131131
return self._discord.http.request(route, **kwargs)
132132

133-
def post_with_files(self, _resp, files: typing.List[discord.File], token):
133+
def request_with_files(self, _resp, files: typing.List[discord.File], token, method, url_ending = ""):
134134

135135
form = aiohttp.FormData()
136136
form.add_field("payload_json", json.dumps(_resp))
137137
for x in range(len(files)):
138138
name = f"file{x if len(files) > 1 else ''}"
139139
sel = files[x]
140140
form.add_field(name, sel.fp, filename=sel.filename, content_type="application/octet-stream")
141-
return self.command_response(token, True, "POST", data=form, files=files)
141+
return self.command_response(token, True, method, data=form, files=files, url_ending=url_ending)
142142

143-
def edit(self, _resp, token, message_id="@original"):
143+
def edit(self, _resp, token, message_id="@original", files: typing.List[discord.File] = None):
144144
"""
145145
Sends edit command response PATCH request to Discord API.
146146
147147
:param _resp: Edited response.
148148
:type _resp: dict
149149
:param token: Command message token.
150150
:param message_id: Message ID to edit. Default initial message.
151+
:param files: Files. Default ``None``
152+
:type files: List[discord.File]
151153
:return: Coroutine
152154
"""
153155
req_url = f"/messages/{message_id}"
156+
if files:
157+
return self.request_with_files(_resp, files, token, "PATCH", url_ending = req_url)
154158
return self.command_response(token, True, "PATCH", url_ending = req_url, json=_resp)
155159

156160

discord_slash/model.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -194,39 +194,50 @@ def __init__(self, *, state, channel, data, _http: http.SlashCommandRequest, int
194194
self._http = _http
195195
self.__interaction_token = interaction_token
196196

197+
async def _slash_edit(self, **kwargs):
198+
"""
199+
An internal function
200+
"""
201+
_resp = {}
202+
203+
content = str(fields.get("content"))
204+
if content:
205+
_resp["content"] = str(content)
206+
207+
embed = fields.get("embed")
208+
embeds = fields.get("embeds")
209+
if embed and embeds:
210+
raise error.IncorrectFormat("You can't use both `embed` and `embeds`!")
211+
if file and files:
212+
raise error.IncorrectFormat("You can't use both `file` and `files`!")
213+
if file:
214+
files = [file]
215+
if embed:
216+
embeds = [embed]
217+
if embeds:
218+
if not isinstance(embeds, list):
219+
raise error.IncorrectFormat("Provide a list of embeds.")
220+
elif len(embeds) > 10:
221+
raise error.IncorrectFormat("Do not provide more than 10 embeds.")
222+
_resp["embeds"] = [x.to_dict() for x in embeds]
223+
224+
allowed_mentions = fields.get("allowed_mentions")
225+
_resp["allowed_mentions"] = allowed_mentions.to_dict() if allowed_mentions else \
226+
self._state.allowed_mentions.to_dict() if self._state.allowed_mentions else {}
227+
228+
await self._http.edit(_resp, self.__interaction_token, self.id, files = files)
229+
230+
delete_after = fields.get("delete_after")
231+
if delete_after:
232+
await self.delete(delay=delete_after)
233+
234+
197235
async def edit(self, **fields):
198236
"""Refer :meth:`discord.Message.edit`."""
199237
try:
200238
await super().edit(**fields)
201239
except discord.Forbidden:
202-
_resp = {}
203-
204-
content = str(fields.get("content"))
205-
if content:
206-
_resp["content"] = str(content)
207-
208-
embed = fields.get("embed")
209-
embeds = fields.get("embeds")
210-
if embed and embeds:
211-
raise error.IncorrectFormat("You can't use both `embed` and `embeds`!")
212-
if embed:
213-
embeds = [embed]
214-
if embeds:
215-
if not isinstance(embeds, list):
216-
raise error.IncorrectFormat("Provide a list of embeds.")
217-
elif len(embeds) > 10:
218-
raise error.IncorrectFormat("Do not provide more than 10 embeds.")
219-
_resp["embeds"] = [x.to_dict() for x in embeds]
220-
221-
allowed_mentions = fields.get("allowed_mentions")
222-
_resp["allowed_mentions"] = allowed_mentions.to_dict() if allowed_mentions else \
223-
self._state.allowed_mentions.to_dict() if self._state.allowed_mentions else {}
224-
225-
await self._http.edit(_resp, self.__interaction_token, self.id)
226-
227-
delete_after = fields.get("delete_after")
228-
if delete_after:
229-
await self.delete(delay=delete_after)
240+
await self._slash_edit(**fields)
230241

231242
async def delete(self, *, delay=None):
232243
"""Refer :meth:`discord.Message.delete`."""

0 commit comments

Comments
 (0)