python - 如何获取 Discord 线程的消息列表?

标签 python discord.py

2021 年 Discord API 添加了一项新功能 - 线程。假设我有一个线程,我想获取线程中的消息列表。我该怎么做?文档说线程就像“子 channel ”,但线程不实现 channel 方法。线程对象的类型就是Message

下面是 Python 程序的示例:

from discord.ext import commands

bot = commands.Bot(command_prefix="$")


@bot.event
async def on_ready():
    for guild in bot.get_all_channels():

        for channel in guild.text_channels:
            async for message in channel.history(limit=100):
                if message.content == "My Thread Name":
                    pass
                    # TODO get individual messages in the thread

最佳答案

alpha 版本有这个:

...
for thread in channel.threads:
    async for message in thread.history()

https://discordpy.readthedocs.io/en/master/api.html#discord.TextChannel.threads

如果你没有使用alpha版本,可以这样安装

$ pip install git+https://github.com/Rapptz/discord.py

您还可以使用库的分支之一:disnakenextcordpycord

关于python - 如何获取 Discord 线程的消息列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71453160/

相关文章:

python - Discord.py 如何删除服务器中的所有角色

python - 如何管理 python 线程结果?

python - 查找字符串列表中最独特的字符

python - 基于 bool np.array 有效地对 np.matrix 进行子集化,但仅直到某个阈值

python - python中的字段继承访问

python-3.x - 使用 Discord.py,有没有办法读取嵌入的消息?

python - 在不和谐消息中搜索多个关键字?不和谐.py 机器人

python - Discord.py - 如何制定角色特定命令?

python - react 编辑,只编辑discord.py中最新的命令

python - 如果我用re.findall 怎么注册才能不分开点