Search code examples
pythondiscorddiscord.pybots

How to make a discord.py event listener that deletes any new thread in a specific channel forum?


I'm trying to make an event that listens for a new thread created in a forum and deletes it if it doesn't match the name requisite

I've tried this, that works for every forum channels, but I can't find a way to make it working just for one specific forum. The problem is on the line if thread.channel.id == forum_channel_id: that gives me an attribute error, saying that Thread object has no channel attribute

@bot.event
async def on_thread_create(thread):
    forum_channel_id = 1069778474815979570
    if thread.channel.id == forum_channel_id:
        return
    if thread.name.startswith("Request #"):
        async for entry in thread.guild.audit_logs(limit=1, action=discord.AuditLogAction.thread_create):
            user = entry.user
            break
        embed = discord.Embed(title="Request Succesfully Done", description="The thread named ''{}'' was succesfully created.".format(thread.name), color=discord.Color.green())
        await user.send(embed = embed)
    else:
        async for entry in thread.guild.audit_logs(limit=1, action=discord.AuditLogAction.thread_create):
            user = entry.user
            break
        embed = discord.Embed(title="Invalid Request", description="The thread named ''{}'' could not be created.".format(thread.name), color=discord.Color.red())
        await user.send(embed = embed)
        await thread.delete()

Any way to solve it?


Solution

  • I mean, I don't see it in the documents either as an attribute...

    ...but it looks like what you really want is the origin ID of where the thread was created, which is what Thread.parent_id will give you.

    parent_id

    The parent TextChannel or ForumChannel ID this thread belongs to.