gpt4 book ai didi

python - 如何在线程上发送消息?

转载 作者:行者123 更新时间:2023-12-05 05:58:19 35 4
gpt4 key购买 nike

我目前有一个命令的代码,该命令接受一个 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 支持线程.

获取线程

1。 Using 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)
2。 Using discord.TextChannel
@bot.command()
async def test(ctx):
thread = ctx.channel.get_thread(thread_id)

线程

1。 Using discord.Guild
@bot.command()
async def test(ctx):
threads = ctx.guild.threads

还有协程active_threads它返回机器人可以看到的所有事件线程

    active_threads = await ctx.guild.active_threads()
2。 Using 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com