【问题标题】:Get all messages sent by a specific user in a channel/server - Discord.py获取频道/服务器中特定用户发送的所有消息 - Discord.py
【发布时间】:2021-05-01 06:22:47
【问题描述】:

我正在尝试找出一种方法来获取由特定用户在频道和/或整个服务器中发送的所有消息(可能需要一段时间)。

我也查看了User.history()Member.history(),但尽管文档没有提及,它只会返回用户的 DM 历史记录。

这里是 Bot 命令代码 sn-p:

@bot.command(aliases=['rq'])
async def randomquote(ctx):

    def check(ctx):
        return ctx.author.id == memberID

    messages = await ctx.channel.history(limit=100, check=check).flatten()
    await ctx.send(f"{random.choice(messages).content}")

我尝试过this answer,但是,check=check 抛出:an exception: TypeError: history() got an unexpected keyword argument 'check',尽管它看起来确实是最干净的解决方案。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    那个答案似乎是错误的,没有这种方法可以在history 中建立检查。此外,如果您想要该用户的所有消息,则需要删除限制。但是,您可以尝试以下方法之一:

    messages = []
    async for message in ctx.channel.history(limit=None):
        if message.author.id == memberID:
            messages += [message]
    
    # Or...
    
    messages = [msg for msg in await ctx.channel.history(limit=None).flatten() if msg.author.id == memberID]
    

    【讨论】:

      【解决方案2】:

      首先您可以获取频道中的所有消息并检查消息的作者是否为用户:

      @bot.command(aliases=['rq'])
      async def randomquote(ctx):
          messages = await ctx.channel.history(limit=100).flatten()
          messages = [m for m in messages if m.author.id == memberID]
          await ctx.send(f"{random.choice(messages).content}")
      

      【讨论】:

      • 或将member : discord.Member 设为必需参数并将memberID 替换为member,这样您就不必一直重新加载机器人/编辑代码。
      猜你喜欢
      • 1970-01-01
      • 2021-04-12
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 2021-03-18
      • 1970-01-01
      • 2021-08-20
      相关资源
      最近更新 更多