Skip to content

Commit a3937f6

Browse files
fix: creating forum threads with files (#2075)
* fix: allow creating forum threads with files * chore: remove unnecessary else branch from conditional Co-authored-by: plun1331 <[email protected]> Signed-off-by: Elliot Cubit <[email protected]> --------- Signed-off-by: Elliot Cubit <[email protected]> Co-authored-by: plun1331 <[email protected]>
1 parent 8950c2e commit a3937f6

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ These changes are available on the `master` branch, but have not yet been releas
123123
`None`. ([#2078](https://github.com/Pycord-Development/pycord/pull/2078))
124124
- Fixed major TypeError when an AuditLogEntry has no user.
125125
([#2079](https://github.com/Pycord-Development/pycord/pull/2079))
126+
- Fixed `HTTPException` when trying to create a forum thread with files.
127+
([#2075](https://github.com/Pycord-Development/pycord/pull/2075))
126128

127129
## [2.4.1] - 2023-03-20
128130

discord/channel.py

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,53 +1274,25 @@ async def create_thread(
12741274
if file is not None and files is not None:
12751275
raise InvalidArgument("cannot pass both file and files parameter to send()")
12761276

1277-
if file is not None:
1278-
if not isinstance(file, File):
1279-
raise InvalidArgument("file parameter must be File")
1280-
1281-
try:
1282-
data = await state.http.send_files(
1283-
self.id,
1284-
files=[file],
1285-
allowed_mentions=allowed_mentions,
1286-
content=message_content,
1287-
embed=embed,
1288-
embeds=embeds,
1289-
nonce=nonce,
1290-
stickers=stickers,
1291-
components=components,
1292-
)
1293-
finally:
1294-
file.close()
1295-
1296-
elif files is not None:
1277+
if files is not None:
12971278
if len(files) > 10:
12981279
raise InvalidArgument(
12991280
"files parameter must be a list of up to 10 elements"
13001281
)
13011282
elif not all(isinstance(file, File) for file in files):
13021283
raise InvalidArgument("files parameter must be a list of File")
13031284

1304-
try:
1305-
data = await state.http.send_files(
1306-
self.id,
1307-
files=files,
1308-
content=message_content,
1309-
embed=embed,
1310-
embeds=embeds,
1311-
nonce=nonce,
1312-
allowed_mentions=allowed_mentions,
1313-
stickers=stickers,
1314-
components=components,
1315-
)
1316-
finally:
1317-
for f in files:
1318-
f.close()
1319-
else:
1285+
if file is not None:
1286+
if not isinstance(file, File):
1287+
raise InvalidArgument("file parameter must be File")
1288+
files = [file]
1289+
1290+
try:
13201291
data = await state.http.start_forum_thread(
13211292
self.id,
13221293
content=message_content,
13231294
name=name,
1295+
files=files,
13241296
embed=embed,
13251297
embeds=embeds,
13261298
nonce=nonce,
@@ -1333,6 +1305,11 @@ async def create_thread(
13331305
applied_tags=applied_tags,
13341306
reason=reason,
13351307
)
1308+
finally:
1309+
if files is not None:
1310+
for f in files:
1311+
f.close()
1312+
13361313
ret = Thread(guild=self.guild, state=self._state, data=data)
13371314
msg = ret.get_partial_message(data["last_message_id"])
13381315
if view:

discord/http.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,15 @@ def start_forum_thread(
11701170
invitable: bool = True,
11711171
applied_tags: SnowflakeList | None = None,
11721172
reason: str | None = None,
1173+
files: Sequence[File] | None = None,
11731174
embed: embed.Embed | None = None,
11741175
embeds: list[embed.Embed] | None = None,
11751176
nonce: str | None = None,
11761177
allowed_mentions: message.AllowedMentions | None = None,
11771178
stickers: list[sticker.StickerItem] | None = None,
11781179
components: list[components.Component] | None = None,
11791180
) -> Response[threads.Thread]:
1180-
payload = {
1181+
payload: dict[str, Any] = {
11811182
"name": name,
11821183
"auto_archive_duration": auto_archive_duration,
11831184
"invitable": invitable,
@@ -1208,12 +1209,38 @@ def start_forum_thread(
12081209

12091210
if rate_limit_per_user:
12101211
payload["rate_limit_per_user"] = rate_limit_per_user
1212+
12111213
# TODO: Once supported by API, remove has_message=true query parameter
12121214
route = Route(
12131215
"POST",
12141216
"/channels/{channel_id}/threads?has_message=true",
12151217
channel_id=channel_id,
12161218
)
1219+
1220+
if files:
1221+
form = [{"name": "payload_json"}]
1222+
1223+
attachments = []
1224+
for index, file in enumerate(files):
1225+
attachments.append(
1226+
{
1227+
"id": index,
1228+
"filename": file.filename,
1229+
"description": file.description,
1230+
}
1231+
)
1232+
form.append(
1233+
{
1234+
"name": f"files[{index}]",
1235+
"value": file.fp,
1236+
"filename": file.filename,
1237+
"content_type": "application/octet-stream",
1238+
}
1239+
)
1240+
1241+
payload["attachments"] = attachments
1242+
form[0]["value"] = utils._to_json(payload)
1243+
return self.request(route, form=form, reason=reason)
12171244
return self.request(route, json=payload, reason=reason)
12181245

12191246
def join_thread(self, channel_id: Snowflake) -> Response[None]:

0 commit comments

Comments
 (0)