作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前有一个命令的代码,该命令接受一个 channel ID,然后是一些文本消息作为输入。然后代码找到 channel 并在其上发送消息。不过Discord刚刚发布了一个新的线程功能,目前官方还没有更新Discord API docs关于机器人如何与线程交互。那么,机器人如何向线程发送消息呢?当 Discord 发布新信息时,请留下答案。这是我之前谈到的代码:
@bot.command()
async def text(ctx, channel_id, *, msg):
channel = bot.get_channel(int(channel_id))
try:
await channel.send(ctx.message.attachments[0].url)
except IndexError:
pass
await channel.trigger_typing()
await channel.send(msg)
最佳答案
自 discord.py 2.0 , discord.Thread
支持线程.
discord.Guild
@bot.command()
async def test(ctx):
thread = ctx.guild.get_thread(thread_id)
还有方法get_channel_or_thread
它返回一个 channel 或一个线程。
channel_or_thread = ctx.guild.get_channel_or_thread(channel_id)
discord.TextChannel
@bot.command()
async def test(ctx):
thread = ctx.channel.get_thread(thread_id)
discord.Guild
@bot.command()
async def test(ctx):
threads = ctx.guild.threads
还有协程active_threads
它返回机器人可以看到的所有事件线程
active_threads = await ctx.guild.active_threads()
discord.TextChannel
@bot.command()
async def test(ctx):
threads = ctx.channel.threads
还有异步迭代器archived_threads
迭代此文本 channel 中的所有存档线程
async for thread in ctx.channel.archived_threads():
# ...
关于python - 如何在线程上发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68549918/
我是一名优秀的程序员,十分优秀!