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

Can't create Forum threads with a file #1949

Closed
3 tasks done
NeloBlivion opened this issue Feb 28, 2023 · 2 comments · Fixed by #2075
Closed
3 tasks done

Can't create Forum threads with a file #1949

NeloBlivion opened this issue Feb 28, 2023 · 2 comments · Fixed by #2075
Assignees
Labels
API Reflection Discords API wasn't correctly reflected in the lib bug Something isn't working priority: high High Priority status: todo This issue needs work
Milestone

Comments

@NeloBlivion
Copy link
Member

Summary

Trying to pass file or files into ForumChannel.create_thread raises a HTTPException due to using the wrong method.

Reproduction Steps

  1. Attempt to create a forum thread with either the file or files parameter

Minimal Reproducible Code

forum_channel_id = ...  # Whatever your ID is
path = "..."  # some image path
forum_channel = bot.get_channel(forum_channel_id)
await forum_channel.create_thread(name="A Thread", file=discord.File(path))

Expected Results

The thread is created with the file included.

Actual Results

Traceback (most recent call last):
  File "/home/container/.local/lib/python3.10/site-packages/discord/channel.py", line 1278, in create_thread
    data = await state.http.send_files(
  File "/home/container/.local/lib/python3.10/site-packages/discord/http.py", line 366, in request
    raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50008): Cannot send messages in a non-text channel

Intents

all

System Information

  • Python v3.10.0-final
  • py-cord v2.4.0-final
  • aiohttp v3.8.1
  • system info: Windows 10 10.0.19045

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

This happens because when file or files is passed in, we use the separate http.send_files method which directs to the forum channel ID, not the thread ID.

        if file is not None:
            ...
            try:
                data = await state.http.send_files(self.id, files=[file], ... )  # ForumChannel ID
            finally:
                file.close()

        elif files is not None:
            ...
            try:
                data = await state.http.send_files(self.id,  files=files, ... )  # ForumChannel ID
            finally:
                for f in files:
                    f.close()
        else:
            data = await state.http.start_forum_thread(...)  # No files arg

This method is only used in the default Messageable.send and isn't really made for this purpose as it's a send method, not for creating threads. The thread creation endpoint supports files directly as part of its message payload, so if I had to guess this was just an oversight that hadn't been considered. I've created this issue for posterity and may look into fixing it myself, but if anyone else wants to go for it then be my guest.

@NeloBlivion NeloBlivion added the bug Something isn't working label Feb 28, 2023
@Lulalaby Lulalaby added priority: high High Priority status: todo This issue needs work API Reflection Discords API wasn't correctly reflected in the lib labels Feb 28, 2023
@Lulalaby Lulalaby added this to the v2.5 milestone Feb 28, 2023
honzajavorek added a commit to juniorguru/junior.guru that referenced this issue Feb 28, 2023
@ghost
Copy link

ghost commented Mar 3, 2023

May I add, same thing happens when a webhook tries creating a forum thread with files attached. It seems that the thread_name entry tries to get included in the payload dict, but when this happens, payload equals to None and throws this error.

File "C:\Users\Stickfab\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\webhook\async_.py", line 1745, in send
    data = await adapter.execute_webhook(
  File "C:\Users\Stickfab\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\webhook\async_.py", line 344, in execute_webhook
    payload["thread_name"] = thread_name
TypeError: 'NoneType' object does not support item assignment

Upon further investigation, it seems that the code turns payload to None when files are included in the message, which contradicts the fact that it needs a payload to create a thread.

# /discord/webhook/async_.py ; lines 610-631
def handle_message_parameters(...) -> ExecuteWebhookParameters:
    ...
    payload = {}
# /discord/webhook/async_.py ; lines 668-670
if files:
        multipart.append({"name": "payload_json", "value": utils._to_json(payload)})
        payload = None
# /discord/webhook/async_.py ; lines 1726-1757
params = handle_message_parameters(...)     # in this case, since it has files, params.payload is None
...
data = await adapter.execute_webhook(
            ...
            payload=params.payload,
            ...
        )
# /discord/webhook/async_.py ; lines 324-344
def execute_webhook(...) -> Response[MessagePayload | None]:
        params = {"wait": int(wait)}
        if thread_id:
            params["thread_id"] = thread_id

        if thread_name:
            payload["thread_name"] = thread_name     # payload is still None, and throws the "TypeError: 'NoneType' object does not support item assignment" exception

@elliotcubit
Copy link
Contributor

Mirroring @honzajavorek's workaround from their previous discussion post here for convenience:

Editing forum threads to add a file upload, like so, is working as expected

thread = await channel.create_thread(...)
message = await thread.fetch_message(thread.id)
await message.edit(file=File(path))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Reflection Discords API wasn't correctly reflected in the lib bug Something isn't working priority: high High Priority status: todo This issue needs work
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants