Python 中的 Discord 机器人正在计算来自用户的消息
Discord bot in Python, counting messages from a user
我目前正在尝试使用 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))
这基本上可以满足我的要求,但是计算所有消息的过程非常缓慢。是否有另一种方法可以达到相同的结果但响应速度更快?
你可以:
使用 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))
它会 return 像这样:
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
我目前正在尝试使用 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))
这基本上可以满足我的要求,但是计算所有消息的过程非常缓慢。是否有另一种方法可以达到相同的结果但响应速度更快?
你可以:
使用
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))
它会 return 像这样:
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