Discord.py:如何浏览频道历史并搜索特定消息? [英] Discord.py: How to go through channel history and search for a specific message?

查看:19
本文介绍了Discord.py:如何浏览频道历史并搜索特定消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我希望机器人浏览频道的消息历史记录,找到包含某些特定文本的消息,然后在同一频道中给出该消息的链接.

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.

通过文档,我想我将使用 async for message in channel.history(params) 但我无法弄清楚我的特定用例如何.

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.

推荐答案

你在正确的轨道上,你可以使用 channel.history 来获取消息.然后只需将您的关键字与消息内容进行比较并使用 jump_url 获取该消息的链接.

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)

这篇关于Discord.py:如何浏览频道历史并搜索特定消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆