Pycord-Development / pycord

Pycord is a modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python
https://docs.pycord.dev
MIT License
2.73k stars 460 forks source link

discord.WebhookMessage.edit() is not handled well in Forum Channels #1979

Closed ghost closed 1 year ago

ghost commented 1 year ago

Summary

When trying to edit a forum channel thread's starter message sent by a webhook, this raises an "Unknown Message" exception

Reproduction Steps

  1. Create a Forum Channel webhook
  2. Create a Webhook object with an algorithm that checks for webhooks in a channel (code will demonstrate)
  3. Create a thread via discord.Webhook.send() and assign a variable to it (wait has to be True)
  4. Attempt to edit the message
  5. Unknown Message should be the exception raised

Minimal Reproducible Code

channel = bot.get_channel(FORUM_CHANNEL_ID) # Forum Channel has to have at least 1 webhook
webhook = await channel.webhooks() # List of webhooks

message = await webhook[0].send("Hello world", thread_name="Thread Name", wait=True)
await message.edit(content="Goodbye world") # Raises "Unknown Message"

Expected Results

The webhook's starter message is expected to be edited.

Actual Results

The following traceback gets printed:

Traceback (most recent call last):
  File "C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 378, in _run_event
    await coro(*args, **kwargs)
  File "...\test_file.py", line 38, in on_ready
    await message.edit(content="Goodbye world")
  File "C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\webhook\async_.py", line 901, in edit
    return await self._state._webhook.edit_message(
  File "C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\webhook\async_.py", line 1947, in edit_message
    data = await adapter.edit_webhook_message(
  File "C:\Users\my_user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\webhook\async_.py", line 219, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message  

Intents

message_content, messages, guilds, emojis, reactions

System Information

Checklist

Additional Context

This seems to be the lines that are causing the issue are the following:

# /discord/webhook/async_.py, Lines 890-894
        thread = MISSING
        if hasattr(self, "_thread_id"):
            thread = Object(self._thread_id)
        elif isinstance(self.channel, Thread):
            thread = Object(self.channel.id)

This code doesn't handle the case where channel.type would be ForumChannel, so thread gets sent as None, and therefore the discord API's link that pycord sends a request to looks like this:

https://discord.com/api/v10/webhooks/[webhook id]/[webhook token]/messages/[message id]

Which, according to the official Discord Developer Docs, is missing the thread_id query string parameter, since it's a Forum Channel and it needs a thread id, otherwise it returns an Unknown Message error. I created a band-aid fix for this:

        thread = MISSING
        if hasattr(self, "_thread_id"):
            thread = Object(self._thread_id)
        elif isinstance(self.channel, Thread):
            thread = Object(self.channel.id)
        elif isinstance(self.channel, ForumChannel): # This fixes the issue where "Unknown Message" would be raised
            thread = Object(self.id) # The thread's ID is the same as the starter message's ID 

Oddly enough, A forum channel's thread and its started message have the same snowflake. Not sure if this is intentional or a discord bug, since this doesn't happen with text channel threads.

Lulalaby commented 1 year ago

Oddly enough, A forum channel's thread and its started message have the same snowflake. Not sure if this is intentional or a discord bug, since this doesn't happen with text channel threads.

That's intentional and happens if:

For your fix: Please pr it