Search code examples
pythondiscordpycord

Why can't I fetch messages with threads?


I want to make a Discord bot with reaction polls and surveys. For this I need my bot to be able to check the reactions of a given message.

My code:

from logic import ds_token, tracked_channel
import discord

bot = discord.Bot(intents=discord.Intents.default())

tracked_msg = int(input("Enter message ID: "))


@bot.event
async def on_ready():
    msg = await bot.get_partial_messageable(tracked_channel).fetch_message(tracked_msg)
    print(msg.reactions)


bot.run(ds_token)

It works just fine with messages without threads:

C:\Users\User\AppData\Local\Programs\Python\Python38\python.exe "E:\Data\bot\thread_test.py" 
Enter message ID: 1023741080266608750
[<Reaction emoji='✅' me=False count=1>]

However, when I try to fetch a message with threads, it raises this exception:

C:\Users\User\AppData\Local\Programs\Python\Python38\python.exe "E:\Data\bot\thread_test.py"  
Enter message ID: 1023742897201360959
Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 382, in _run_event
    await coro(*args, **kwargs)
  File "E:\Data\bot\thread_test.py", line 11, in on_ready
    msg = await bot.get_partial_messageable(tracked_channel).fetch_message(tracked_msg)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\abc.py", line 1602, in fetch_message
    return self._state.create_message(channel=channel, data=data)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\state.py", line 1693, in create_message
    return Message(state=self, channel=channel, data=data)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\message.py", line 776, in __init__
    self.thread = Thread(guild=self.guild, state=self._state, data=data["thread"])
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\threads.py", line 160, in __init__
    self._from_data(data)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\threads.py", line 190, in _from_data
    if thread := self.guild.get_thread(self.id) and data.pop("_invoke_flag", False):
AttributeError: 'NoneType' object has no attribute 'get_thread'

Why does this happen and how may I fix this?


Solution

  • I came up with a frankly stupid but working solution: modify the source file (discord\message.py) to just ignore the exception:

            self.thread: Optional[Thread]
            try:
                self.thread = Thread(guild=self.guild, state=self._state, data=data["thread"])
            except KeyError:
                self.thread = None
            except AttributeError:  # New code
                self.thread = None
    

    Now my bot returns a satisfactory result:

    C:\Users\User\AppData\Local\Programs\Python\Python38\python.exe "E:\Data\bot\thread_test.py" 
    Enter message ID: 1023742897201360959
    [<Reaction emoji='❌' me=False count=1>]
    

    My modifications may cause complications in the future and I definetly want to find a solution that won't require me to edit the library's source code, but for now this is all I can do.