#Python Discord Bot

1 messages · Page 1 of 1 (latest)

distant wrenBOT
#
gabchan has been warned

Reason: Duplicated text

wet latch
#

huh

#

How can I make my discord bot send a message on a thread? I have many channels, all of them have thread, and it's name is consistent across all channels, and I want to make it send a message whenever I issue one specific command.

               print(f"Thread not found. Creating new thread: {THREAD_NAME}") #variable declated earlier into the code
               thread = await interaction.channel.create_thread(
                   name=THREAD_NAME, 
                   type=discord.ChannelType.public_thread
               )
           else:
               if thread.archived:
                   print(f"Unarchiving thread: {thread.name}")
                   await thread.edit(archived=False)

           additional_message = f"Thread Updated"
           await thread.send(additional_message)

       except Exception as e:
           print(f":warning: Error handling thread: {e}")
           await interaction.followup.send(
               ":warning: Failed to update thread",
               ephemeral=True
           )
           return
light cove
wet latch
light cove
#

thread id are unique?

wet latch
#

all of them got stored id's on a database in sqlite3

wet latch
light cove
#

as i said thread ids r unique and u can differ which is which therefore i dont get why u cant just send the wanted message to that thread

wet latch
#

well, I don't have them stored alongside with the channel ID's

light cove
#

so u have the channel id stored but not the thread ids?

wet latch
light cove
#

then get the specific channel and then use .threads

light cove
# wet latch yea

if theres no other way, i think you can leave a message and try to get a certain message/apply tags from those consistently name threads to filter them out?

wet latch
# light cove then get the specific channel and then use ``.threads``

yeah, got it now, many thanks, had to do this way ->

            channel = interaction.channel

            thread = discord.utils.get(channel.threads, name=THREAD_NAME)

            if thread is None:
                print(f"Thread '{THREAD_NAME}' not found in channel {channel.name}. Creating a new thread...")
                thread = await channel.create_thread(
                    name=THREAD_NAME,
                    type=discord.ChannelType.public_thread
                )
                print(f"Created new thread: {thread.name}")
            else:
                print(f"Found existing thread: {thread.name}")

                if thread.archived:
                    print(f"Unarchiving thread: {thread.name}")
                    await thread.edit(archived=False)

            additional_message = f"Thread Updated by {interaction.user.display_name}"
            await thread.send(additional_message)
            print(f"Thread updated successfully!")

        except Exception as e:
            print(f":warning: Error handling thread: {e}")
            await interaction.followup.send(
                "Failed to update thread!",
                ephemeral=True
            )
#

🙏