You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在discord.py中提及线程(Thread)?

如何在discord.py中提及线程(Thread)?

我懂你遇到的问题了——在Pycord里用thread.mention就能直接生成可点击的线程提及,但换到discord.py就不管用了,对吧?其实解决方法很简单,有两种方式可以实现和Pycord一模一样的效果:

方法一:手动构造线程提及格式

Discord里的提及本质上都是特定格式的字符串,线程的提及格式是<#{线程ID}>。所以你可以直接用线程对象的id来构造这个字符串,修改后的代码如下:

class Example:
    @app_commands.command(name="chat", description="some desc")
    async def chat(self, ctx, msg):
        await ctx.response.defer(ephemeral=True)
        thread = await self.bot.fetch_channel(user_thread[1])
        # 手动构造线程提及
        thread_mention = f"<#{thread.id}>"
        await ctx.respond(f"Hello {ctx.author.mention}\nThread: {thread_mention}", ephemeral=True)

方法二:确保discord.py版本支持thread.mention

如果你的discord.py版本比较旧,可能确实没有Thread.mention这个属性。你可以先把discord.py更新到最新稳定版,执行以下命令:

pip install -U discord.py

更新完成后,再尝试使用thread.mention,应该就能和Pycord里一样正常工作了。

两种方法都能达成你想要的效果,选哪种都没问题~

备注:内容来源于stack exchange,提问作者syrok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 11:24:28