2017-06-11 35 views
1

現在、私は、サーバー上のチャンネルから統計を収集するpythonでボットを作ろうとしています。私は、ユーザーが特定のチャンネルで送信したメッセージの数を確認したい。現在、私のコードは次のようになります。ユーザからのメッセージ数をカウントする、PythonのDiscordボット

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)) 

これは、基本的にはしかし、すべてのメッセージを計算するためのプロセスは、痛々しいほど遅いです、私が欲しいものを行います。同じ結果を達成する別の方法がありますが、より速い応答がありますか?あなたはできる

答えて

0

:あなたは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

関連する問題