Pycord: Copy channel messages and resend them in a thread

51 views Asked by At

Okay so, I will explain what Im doing here: I want my bot to read a specific channel, and any message sent in that channel (no matter what, who, pic, gif, embed) the bot will send it in a thread itself, so I've asked the AI and I got this:

itents.messages = True
@client.event
async def on_message(message):
    if message.channel.id == 1166507853314539561:
        thread = client.get_channel(1209617468188925993)

        if message.content:
            await thread.send(message.content)
        
        for embed in message.embeds:
            await thread.send(embed=embed)

With this code the bot is reading the source channel, but he is not reading the thread channel, I think the client.get_channel is not working with a thread, I can use a channel ID and it works, but dont send the message, so the problems are:

  1. client.get_channel doesnt work with a thread
  2. The bot is not sending the readed messages in the second channel.

this is just a part of the code, the imports and the startup for the bot is ready.

I need my bot to read a channel and sent the messages from that channel in a threat.

1

There are 1 answers

0
Blue Robin On

Why you aren't getting the thread

client.get_channel fetches from the cache and only gets the channel. What we want is the thread.

What to do instead

channel.get_thread(thread_id) will work instead.

Code in context:

@client.event
async def on_message(message):
    if message.channel.id == 1166507853314539561:
        channel = client.get_channel(channelID)  # replace with channel ID of where the thread is 
        if channel is None:
            client.fetch_channel(channelID)  # fetch if not found in cache
        
        thread = channel.get_thread(thread_id)  # use the thread ID

        if message.content:
            await thread.send(message.content)
        
        for embed in message.embeds:
            await thread.send(embed=embed)

Documentation:

Note: I haven't tested the code I made before, but I believe it should work