【问题标题】:Discord bot in Python, counting messages from a userPython中的不和谐机器人,计算来自用户的消息
【发布时间】:2017-11-13 05:59:50
【问题描述】:

我目前正在尝试使用 python 制作一个从服务器上的频道收集统计信息的机器人。我想查看用户在某个频道中发送了多少条消息。目前我的代码如下所示:

if message.content.startswith('!stat'):
        mesg = await client.send_message(message.channel, 'Calculating...')
        counter = 0
        async for msg in client.logs_from(message.channel, limit=9999999):
            if msg.author == message.author:
                counter += 1
        await client.edit_message(mesg, '{} has {} messages in {}.'.format(message.author, str(counter), message.channel))

这基本上可以满足我的要求,但是计算所有消息的过程非常缓慢。是否有其他方法可以达到相同的结果但响应速度更快?

【问题讨论】:

    标签: python bots discord


    【解决方案1】:

    你可以:

    • 当您使用client.logs_from 时,降低limit。您也可以忽略限制,因为机器人很可能会尝试获取不存在的消息。

    • 每 x 条消息与用户互动。示例:

      counter = 0
      repeat = 0
      for x in range(0, 4): # Repeats 4 times
          async for msg in client.logs_from(message.channel, limit=500):
              if msg.author == message.author:
                  counter += 1
          repeat += 1
      await client.send_message(message.channel, "{} has {} out of the first {} messages in {}".format(message.author, str(counter), 500*repeat, message.channel))
      

    它会返回如下内容:

    User has 26 out of the first 500 messages in channel
    User has 51 out of the first 1000 messages in channel
    

    discord.py API 参考:http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.logs_from

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 2018-11-19
      • 1970-01-01
      • 2020-10-03
      • 2020-05-11
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      相关资源
      最近更新 更多