python - 如何获取用户在服务器 [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 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.')
推荐阅读
- java - 为什么在导入 com.sun.management.OperatingSystemMXBean 时显示错误
- outlook - 带有多个日历的 Outlook:如何使用 VBA 确定选定的日历?
- reactjs - npm create-react-app 等待很长时间后无法完成
- python - 如何从我使用 python 收集的这个请求中刮取歌曲标题
- python - 使用 python 打印电子邮件正文
- javascript - 使用 Apple JS 问题签名
- html - 是否所有 20x http 响应都有正文,所有非 20x http 响应都没有正文?
- ios - Realm Swift:关于基于查询的公共数据库的问题
- sql-server - 尝试从 VBA 运行 SQL 脚本
- php - 父子sql表在php中显示为表