【问题标题】:Discord py fetching messagesDiscord py 获取消息
【发布时间】:2020-12-17 03:32:59
【问题描述】:

我正在尝试从公会的特定频道中获取用户的所有消息。但是,即使用户在频道/公会中发送了超过 30 条消息,它也只能从用户那里收到一条消息。

async def check(channel):
    fetchMessages = await channel.history().find(lambda m: m.author.id == 627862713242222632)
    print(fetchMessages.content)

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    问题在于.find().get() 只返回与您的条件匹配的第一个条目。您可以改为 flatten() 来自该频道的消息,这将为您提供消息列表,然后过滤掉属于您指定的 ID 的消息。请务必检查文档的链接。

    @client.command()
    async def check(ctx, channel:discord.TextChannel): # Now you can tag the channel
        messages = await channel.history().flatten()
        # messages is now list of message objects.
    
        for m in messages:
            if m.author.id == 627862713242222632: # ID you provided
                print(m.content) # one message at the tim
    

    或者另一种方法是使用filter()

    @client.command()
    async def check(ctx, channel:discord.TextChannel):
        def user_filter(message):
            return message.author.id == 627862713242222632
    
        messages = [m.content async for m in channel.history().filter(user_filter)]
        print(messages) # prints the list we get from the line above
    

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 2020-04-07
      • 2021-06-17
      • 1970-01-01
      • 2021-05-28
      • 2021-06-14
      • 2020-10-01
      • 2019-10-02
      • 2021-05-09
      相关资源
      最近更新 更多