Discord.py: How to go through channel history and search for a specific message?

Viewed 17565

Essentially, I want the bot to go through the channel's message history, find a message containing some particular text, and then give out the link to the message in the same channel.

Going through the documentation, I imagine I will make use of async for message in channel.history(params) but I cannot figure out how for my particular use case.

1 Answers

You're on the right track, you can use channel.history to get the messages. Then just compare your keyword with the message content and use jump_url to get the link to that message.

@client.command()
async def keyword(ctx, *, word: str):
    channel = client.get_channel(730839966472601622)
    messages = await ctx.channel.history(limit=200).flatten()

    for msg in messages:
        if word in msg.content:
            print(msg.jump_url)
Related