首页 > 解决方案 > 如何获取用户在服务器 [Discord.py] 中发送的所有消息?

问题描述

如何在不使用数据库或列表的情况下获取用户在服务器中发送的所有消息?
也许有这样的方法:

messages = await message.guild.find_messages(author=message.author)
await message.channel.send(f"You sent {len(messages)} messages in this server")

标签: pythondiscorddiscord.py

解决方案


您可以history()在这样的频道中使用该功能:

@client.command()
async def history(ctx, member: discord.Member):
    counter = 0
    async for message in ctx.channel.history(limit = 100):
        if message.author == member:
            counter += 1

    await ctx.send(f'{member.mention} has sent **{counter}** messages in this channel.')

这只会读取该100频道中的最后一条消息。您可以设置limit一些高得离谱的值,但这就是机器人响应所需的时间。


对于服务器,您可以遍历服务器中的所有通道,并且在每次迭代中,再次遍历所有消息。这将花费很长时间,但没有其他办法。

所以你必须把上面的代码放在另一个循环中,它看起来像这样:

@client.command()
async def history(ctx, member: discord.Member):
    counter = 0
    for channel in ctx.guild.channels:
        async for message in channel.history(limit = 100):
            if message.author == member:
                counter += 1

    await ctx.send(f'{member.mention} has sent **{counter}** messages in this server.')

推荐阅读