旧答案discord.py async(重写前)
Use log_froms http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.logs_from从频道获取消息。
and use get_all_channels http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.get_all_channels通过所有渠道。
然后在结果中搜索作者的最新版本。您必须在每个渠道中浏览合理的数量,直到找到该人的消息,然后停止。比较每个通道的第一个以获得最新时间。
为了将来获得更好的帮助,请考虑加入“discord api”discord 服务器 https://discord.gg/discord-api
编辑:与discord.py重写兼容的方法(+1.0)
您可以使用channel.history() https://discordpy.readthedocs.io/en/v1.0.0/api.html#discord.TextChannel.history获取频道的历史记录。默认情况下,它仅从频道中获取最新的 100 条消息。您可以通过以下方式增加此限制limit
关键词。 (例如。channel.history(limit = 200)
您可以将其与find https://discordpy.readthedocs.io/en/v1.0.0/api.html#discord.AsyncIterator.find异步迭代器仅获取具有您正在查找的 id 的用户的消息await channel.history().find(lambda m: m.author.id == users_id)
.
然后,您需要循环遍历服务器中的每个文本通道,并通过将它们与先前获取的消息进行比较并保留较新创建的消息来查找通道中的最新消息。
查找用户最新消息的示例命令。
@commands.command()
async def lastMessage(self, ctx, users_id: int):
oldestMessage = None
for channel in ctx.guild.text_channels:
fetchMessage = await channel.history().find(lambda m: m.author.id == users_id)
if fetchMessage is None:
continue
if oldestMessage is None:
oldestMessage = fetchMessage
else:
if fetchMessage.created_at > oldestMessage.created_at:
oldestMessage = fetchMessage
if (oldestMessage is not None):
await ctx.send(f"Oldest message is {oldestMessage.content}")
else:
await ctx.send("No message found.")
在我的测试中,这是一个相当慢的操作,因为必须发出许多不和谐的请求,但应该可以工作。