首页 > 解决方案 > 如何使用 discord.py 在不和谐服务器的特定频道中获取所有聊天消息?

问题描述

我的机器人具有管理员权限,所以我只是想知道是否可以从 1 个频道获取所有消息,例如 #general。

标签: discordbotsdiscord.pydiscord.py-rewrite

解决方案


频道历史

这是可能的,有一个函数可以做到这一点:channel.history()

此函数可用于检索通道中发送的所有消息。但是,如果您要检索包含数千条消息的频道中的所有消息,则速度非常慢。


例子

此示例显示了如何检索在某个通道中发送的每条消息:

async def get_history_of_channel(channel):
    messages = await channel.history(limit=None) # adding None lets us retrieve every message
    for message in messages:
        # do something with that message
        print(message.content)

参考


推荐阅读