Vadim Melnikov
Vadim Melnikov

Reputation: 100

How to get all messages that user sent in server [Discord.py]?

How to get all messages that user sent in the server without using databses or lists?
Maybe there are method like this:

messages = await message.guild.find_messages(author=message.author)
await message.channel.send(f"You sent {len(messages)} messages in this server")

Upvotes: 1

Views: 6167

Answers (1)

Sujit
Sujit

Reputation: 1792

You can use the history() function in a channel like this:

@client.command()
async def history(ctx, member: discord.Member):
    counter = 0
    async for message in ctx.channel.history(limit = 100):
        if message.author == member:
            counter += 1

    await ctx.send(f'{member.mention} has sent **{counter}** messages in this channel.')

This will only read the last 100 messages in that channel. You can set limit to some ridiculously high value, but that's how long it'll take for the bot to respond.


For the server, you could iterate through all the channels in the server, and in each iteration, iterate through all the messages again. This will take an awful long time, but there's no other way.

So you'll have to put that above code inside another loop and it'll look like this:

@client.command()
async def history(ctx, member: discord.Member):
    counter = 0
    for channel in ctx.guild.channels:
        async for message in channel.history(limit = 100):
            if message.author == member:
                counter += 1

    await ctx.send(f'{member.mention} has sent **{counter}** messages in this server.')

Upvotes: 1

Related Questions