Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: creating forum threads with files #2075

Merged
merged 3 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -123,6 +123,8 @@ These changes are available on the `master` branch, but have not yet been releas
`None`. ([#2078](https://github.com/Pycord-Development/pycord/pull/2078))
- Fixed major TypeError when an AuditLogEntry has no user.
([#2079](https://github.com/Pycord-Development/pycord/pull/2079))
- 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 @@
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 @@
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
32 changes: 30 additions & 2 deletions discord/http.py
Expand Up @@ -1170,14 +1170,15 @@
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,40 @@

if rate_limit_per_user:
payload["rate_limit_per_user"] = rate_limit_per_user

# 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)

if files:
form = [{"name": "payload_json"}]

Check warning on line 1221 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1220-L1221

Added lines #L1220 - L1221 were not covered by tests

attachments = []
for index, file in enumerate(files):
attachments.append(

Check warning on line 1225 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1223-L1225

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

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
{
"name": f"files[{index}]",
"value": file.fp,
"filename": file.filename,
"content_type": "application/octet-stream",
}
)

payload["attachments"] = attachments
form[0]["value"] = utils._to_json(payload)
return self.request(route, form=form, reason=reason)

Check warning on line 1243 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1241-L1243

Added lines #L1241 - L1243 were not covered by tests
else:
return self.request(route, json=payload, reason=reason)

Check warning on line 1245 in discord/http.py

View check run for this annotation

Codecov / codecov/patch

discord/http.py#L1245

Added line #L1245 was not covered by tests
elliotcubit marked this conversation as resolved.
Show resolved Hide resolved

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