Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取用户在服务器[Discord.py]中发送的所有消息?_Python_Discord_Discord.py - Fatal编程技术网

Python 如何获取用户在服务器[Discord.py]中发送的所有消息?

Python 如何获取用户在服务器[Discord.py]中发送的所有消息?,python,discord,discord.py,Python,Discord,Discord.py,如何在不使用数据库或列表的情况下获取用户在服务器中发送的所有消息? 也许有这样的方法: messages = await message.guild.find_messages(author=message.author) await message.channel.send(f"You sent {len(messages)} messages in this server") 您可以在如下频道中使用history()函数: @client.command() async

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

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

您可以在如下频道中使用
history()
函数:

@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.')
@client.command()
异步def历史记录(ctx,成员:discord.member):
计数器=0
ctx.channel.history中消息的异步(限制=100):
如果message.author==成员:
计数器+=1
等待ctx.send(f'{member.antify}已在此通道中发送**{counter}**消息。“)
这将只读取该频道中最后的
100
消息。您可以将
limit
设置为某个高得离谱的值,但这就是bot响应所需的时间


对于服务器,您可以迭代服务器中的所有通道,并且在每次迭代中,再次迭代所有消息。这将需要很长时间,但别无选择

因此,您必须将上述代码放入另一个循环中,它将如下所示:

@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.')