Search code examples
pythondiscorddiscord.py

How to read messages in a thread with discord.py?


After looking through the docs of discord.py for a painful amount of time, I still can't find a way to get messages from a thread. Hopefully there is a function for this, but am open to other hacky things to get this working.

async for message in channel.history(after=start, before=end):
    if message.type == discord.MessageType.public_thread:
        #await for m in message.fetch_messages()????

Solution

  • forum = bot.get_channel(111111111111)  # forum channel ID
    thread = discord.utils.get(
        forum.threads, id=22222222222)  # thread channel ID
    )
    
    # instead of the above could simply do:
    # thread = bot.get_channel(22222222)  # thread channel ID
    
    async for message in thread.history(after=start, before=end):
        ...  # do something with message