I'm currently using this code to check the number of messages sent by a user but this approach is very slow, it is taking 1 - 2 min to calculate for each user
user = discord.utils.find(lambda m: m.id== j, channel.guild.members)
async for message in channel.history(limit = 100000):
if message.author == user:
userMessages.append(message.content)
print(len(userMessages))
is there any other fast approach to doing this?
CodePudding user response:
Counting messages
You can use on_message
event to count messages.
message_count = {}
@client.event
async def on_message(message):
global message_count
if message.guild.id not in message_count:
message_count[message.guild.id] = {}
try:
message_count[message.guild.id][message.author.id] = 1
except KeyError:
message_count[message.guild.id][message.author.id] = 1
client.process_commands(message)
And then use
member = something # specify member here
try:
count = message_count[member.guild.id][member.id]
except KeyError:
count = 0
# now `count` is count of messages from `member`
To get count of messages from member.
Note: Message count resets on your bot restart but this solution would work so quickly.
Database
Another way to do what you want is use any database to store message count from different members.