簡體   English   中英

獲取用戶使用 discord.py 發送的最后一條消息?

[英]Get the last message a user sent using discord.py?

我想知道是否有辦法讓機器人使用 Python 中的 discord.py 獲取用戶在服務器聊天中發送的最后一條消息? 非常感謝

舊答案 discord.py 異步(重寫前)

使用log_froms從頻道獲取消息。

並使用get_all_channels遍歷所有通道。

然后在結果中搜索作者的最新版本。 您必須以合理的數量瀏覽每個渠道,直到找到來自該人的消息,然后停止。 比較每個通道的第一個以獲得最新時間。

為了將來獲得更好的幫助,請考慮加入“discord api”不和諧服務器

編輯:與 discord.py 重寫兼容的方法(+1.0)

您可以使用channel.history()來獲取頻道的歷史記錄。 默認情況下,它僅從通道中獲取最新的 100 條消息。 您可以使用limit關鍵字增加此限制。 (例如channel.history(limit = 200)

您可以將其與find異步迭代器結合使用,以僅從用戶處獲取帶有您正在尋找的 ID 的消息await channel.history().find(lambda m: m.author.id == users_id)

然后,您需要遍歷服務器中的每個文本通道,並通過將它們與先前獲取的消息進行比較並保持創建的較新的消息來查找通道中的最新消息。

查找用戶最新消息的示例命令。

@commands.command()
async def lastMessage(self, ctx, users_id: int):
    oldestMessage = None
    for channel in ctx.guild.text_channels:
        fetchMessage = await channel.history().find(lambda m: m.author.id == users_id)
        if fetchMessage is None:
            continue


        if oldestMessage is None:
            oldestMessage = fetchMessage
        else:
            if fetchMessage.created_at > oldestMessage.created_at:
                oldestMessage = fetchMessage

    if (oldestMessage is not None):
        await ctx.send(f"Oldest message is {oldestMessage.content}")
    else:
        await ctx.send("No message found.")

在我的測試中,這是一個相當緩慢的操作,因為必須提出許多不和諧的請求,但應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM