Skip to content

Commit

Permalink
fix: allow creating forum threads with files
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcubit committed May 14, 2023
1 parent 6a69f66 commit 4fa1343
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -113,6 +113,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2048](https://github.com/Pycord-Development/pycord/pull/2048))
- Fixed the Slash command syncronization method `indiviual`.
([#1925](https://github.com/Pycord-Development/pycord/pull/1925))
- Fixed `HttpException` when trying to create a forum thread with files.
([#2075](https://github.com/Pycord-Development/pycord/pull/2075))

## [2.4.1] - 2023-03-20

Expand Down
49 changes: 13 additions & 36 deletions discord/channel.py
Expand Up @@ -1274,53 +1274,25 @@ async def create_thread(
if file is not None and files is not None:
raise InvalidArgument("cannot pass both file and files parameter to send()")

if file is not None:
if not isinstance(file, File):
raise InvalidArgument("file parameter must be File")

try:
data = await state.http.send_files(
self.id,
files=[file],
allowed_mentions=allowed_mentions,
content=message_content,
embed=embed,
embeds=embeds,
nonce=nonce,
stickers=stickers,
components=components,
)
finally:
file.close()

elif files is not None:
if files is not None:

Check warning on line 1277 in discord/channel.py

View check run for this annotation

Codecov / codecov/patch

discord/channel.py#L1277

Added line #L1277 was not covered by tests
if len(files) > 10:
raise InvalidArgument(
"files parameter must be a list of up to 10 elements"
)
elif not all(isinstance(file, File) for file in files):
raise InvalidArgument("files parameter must be a list of File")

try:
data = await state.http.send_files(
self.id,
files=files,
content=message_content,
embed=embed,
embeds=embeds,
nonce=nonce,
allowed_mentions=allowed_mentions,
stickers=stickers,
components=components,
)
finally:
for f in files:
f.close()
else:
if file is not None:
if not isinstance(file, File):
raise InvalidArgument("file parameter must be File")
files = [file]

Check warning on line 1288 in discord/channel.py

View check run for this annotation

Codecov / codecov/patch

discord/channel.py#L1285-L1288

Added lines #L1285 - L1288 were not covered by tests

try:

Check warning on line 1290 in discord/channel.py

View check run for this annotation

Codecov / codecov/patch

discord/channel.py#L1290

Added line #L1290 was not covered by tests
data = await state.http.start_forum_thread(
self.id,
content=message_content,
name=name,
files=files,
embed=embed,
embeds=embeds,
nonce=nonce,
Expand All @@ -1333,6 +1305,11 @@ async def create_thread(
applied_tags=applied_tags,
reason=reason,
)
finally:
if files is not None:
for f in files:
f.close()

Check warning on line 1311 in discord/channel.py

View check run for this annotation

Codecov / codecov/patch

discord/channel.py#L1309-L1311

Added lines #L1309 - L1311 were not covered by tests

ret = Thread(guild=self.guild, state=self._state, data=data)
msg = ret.get_partial_message(data["last_message_id"])
if view:
Expand Down
29 changes: 27 additions & 2 deletions discord/http.py
Expand Up @@ -1170,14 +1170,15 @@ def start_forum_thread(
invitable: bool = True,
applied_tags: SnowflakeList | None = None,
reason: str | None = None,
files: Sequence[File] | None = None,
embed: embed.Embed | None = None,
embeds: list[embed.Embed] | None = None,
nonce: str | None = None,
allowed_mentions: message.AllowedMentions | None = None,
stickers: list[sticker.StickerItem] | None = None,
components: list[components.Component] | None = None,
) -> Response[threads.Thread]:
payload = {
payload: dict[str, Any] = {

Check warning on line 1181 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1181

Added line #L1181 was not covered by tests
"name": name,
"auto_archive_duration": auto_archive_duration,
"invitable": invitable,
Expand Down Expand Up @@ -1208,13 +1209,37 @@ def start_forum_thread(

if rate_limit_per_user:
payload["rate_limit_per_user"] = rate_limit_per_user

form = [{"name": "payload_json"}]
if files:
attachments = []
for index, file in enumerate(files):
attachments.append(

Check warning on line 1217 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1213-L1217

Added lines #L1213 - L1217 were not covered by tests
{
"id": index,
"filename": file.filename,
"description": file.description,
}
)
form.append(

Check warning on line 1224 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1224

Added line #L1224 was not covered by tests
{
"name": f"files[{index}]",
"value": file.fp,
"filename": file.filename,
"content_type": "application/octet-stream",
}
)
payload["attachments"] = attachments

Check warning on line 1232 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1232

Added line #L1232 was not covered by tests

form[0]["value"] = utils._to_json(payload)

Check warning on line 1234 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1234

Added line #L1234 was not covered by tests

# TODO: Once supported by API, remove has_message=true query parameter
route = Route(
"POST",
"/channels/{channel_id}/threads?has_message=true",
channel_id=channel_id,
)
return self.request(route, json=payload, reason=reason)
return self.request(route, form=form, reason=reason)

Check warning on line 1242 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1242

Added line #L1242 was not covered by tests

def join_thread(self, channel_id: Snowflake) -> Response[None]:
return self.request(
Expand Down

0 comments on commit 4fa1343

Please sign in to comment.