8auuuuuer
8auuuuuer

Reputation: 51

Discord py fetching messages

I'm trying to fetch all the messages from a user in a specific channel of a guild. However, it only gets one message from the user, even though the user has sent over 30 messages in the channel/guild.

async def check(channel):
    fetchMessages = await channel.history().find(lambda m: m.author.id == 627862713242222632)
    print(fetchMessages.content)

Upvotes: 0

Views: 205

Answers (1)

AbdurJ
AbdurJ

Reputation: 1033

The problem is that .find() and .get() only returns the very first entry that matches your condition. You can instead flatten() messages from that channel, which will give you a list of messages and then filter out the messages that belongs to the ID you specified. Make sure to check the links to the documentation.

@client.command()
async def check(ctx, channel:discord.TextChannel): # Now you can tag the channel
    messages = await channel.history().flatten()
    # messages is now list of message objects.

    for m in messages:
        if m.author.id == 627862713242222632: # ID you provided
            print(m.content) # one message at the tim

Or an alternate way would be using filter().

@client.command()
async def check(ctx, channel:discord.TextChannel):
    def user_filter(message):
        return message.author.id == 627862713242222632

    messages = [m.content async for m in channel.history().filter(user_filter)]
    print(messages) # prints the list we get from the line above

Upvotes: 1

Related Questions