Skip to content
Advertisement

Get all messages sent by a specific user in a channel/server – Discord.py

I’m trying to figure out a way to get all messages sent by a specific user in a channel and/or the whole server (would probably take a while for that).

I have also looked into User.history() and Member.history(), but despite the documentation not mentioning it, it will only return a User’s DM history.

Here is the Bot Command code snippet:

@bot.command(aliases=['rq'])
async def randomquote(ctx):

    def check(ctx):
        return ctx.author.id == memberID

    messages = await ctx.channel.history(limit=100, check=check).flatten()
    await ctx.send(f"{random.choice(messages).content}")

I’ve tried this answer, however, the check=check throws: an exception: TypeError: history() got an unexpected keyword argument 'check' ,although it does look like the cleanest solution.

Advertisement

Answer

That answer appears to be wrong, there is no such way to build checks into history. Also, if you want all the messages by that user, you would need to remove the limit. You could, however, try one of these:

messages = []
async for message in ctx.channel.history(limit=None):
    if message.author.id == memberID:
        messages += [message]

# Or...

messages = [msg for msg in await ctx.channel.history(limit=None).flatten() if msg.author.id == memberID]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement