获取 discord 频道中的活动线程 (Discord.py / Pycord)

Get active threads in a discord channel (Discord.py / Pycord)

提问人:syrok 提问时间:11/18/2023 更新时间:11/18/2023 访问量:31

问:

我在 discord 频道上获取活动线程时遇到问题。关于它的信息不多,或者我只是找不到它。所以也许这甚至是不可能的 idk

我的代码:

import discord
from tools.config import TOKEN

bot = discord.Bot()

@bot.event
async def on_ready():
    print(f"{bot.user} is ready and online!")

@bot.slash_command(name="chat", description="some desc")
async def chat(ctx, msg):
    channel = bot.get_channel(CHANNEL_ID)
    user_name = ctx.author
    await ctx.response.defer(ephemeral=True)
    thread = await channel.create_thread(name=f"Thread was created {user_name}", type=None)
    await thread.send("Message in a thread")

    await ctx.respond(f'Some message with tagging after creating a thread. {user_name.mention}')

bot.run(TOKEN)

我试图实现的是: 用户执行斜杠命令,然后机器人创建一个线程,在该线程中完成与用户的所有通信,但在创建线程之前,机器人需要验证用户是否不在当前通道的任何活动线程中。为此,我需要获取当前通道中的活动线程。

python-3.x discord discord.py pycord

评论


答:

1赞 dfop02 11/18/2023 #1

首先,确保您使用的是 Discord.py 2.0+,其中有重要的更改和功能仅存在于最新版本中。

我想我找到了解决你问题的方法。首先从频道获取所有线程,如果我很好地理解了已经获得的文档,而不是存档的线程(这意味着它们仍然处于活动状态),然后检查您的用户是否在线程成员之间。channel.threads

async def user_has_no_thread(ctx):
    threads = ctx.channel.threads
    if none(ctx.author in thread.members for thread in threads):
        return True
    return False

您还可以在使用时定义一个更快的速度,以确保它们很快就会被存档,我猜默认情况下大约是 3 天。如果您想了解更多关于该功能的信息,请看这里auto_archive_durationchannel.create_thread()

此外,请确保您已为此作品启用成员意图

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)