如何创建一个discord.py事件监听器,用于删除特定频道论坛中的任何新主题?

huangapple go评论151阅读模式
英文:

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

问题

我试图创建一个事件,用于监听在论坛中创建的新线程,并在不符合名称要求时删除它

我已经尝试过这个方法,它适用于所有论坛频道,但我找不到一种方法,只使其在一个特定的论坛中工作。问题出在这一行 if thread.channel.id == forum_channel_id: 上,它给我一个属性错误,说 Thread 对象没有 channel 属性。

@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.", 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.", color=discord.Color.red())
        await user.send(embed=embed)
        await thread.delete()

有解决方法吗?

英文:

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?

答案1

得分: 0

...但在文件中,我也看不到它作为一个属性...

...但它 看起来 像是你真正想要的是该线程创建的原始 ID,这就是 Thread.parent_id 将为你提供的内容。

parent_id

该线程所属的父TextChannelForumChannel的ID。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月9日 01:54:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定