簡體   English   中英

如何獲取用戶在服務器 [Discord.py] 中發送的所有消息?

[英]How to get all messages that user sent in server [Discord.py]?

如何在不使用數據庫或列表的情況下獲取用戶在服務器中發送的所有消息?
也許有這樣的方法:

messages = await message.guild.find_messages(author=message.author)
await message.channel.send(f"You sent {len(messages)} messages in this server")

您可以在這樣的頻道中使用history() function:

@client.command()
async def history(ctx, member: discord.Member):
    counter = 0
    async for message in ctx.channel.history(limit = 100):
        if message.author == member:
            counter += 1

    await ctx.send(f'{member.mention} has sent **{counter}** messages in this channel.')

這只會讀取該頻道中的最后100條消息。 您可以將limit設置為一些高得離譜的值,但這就是機器人響應所需的時間。


對於服務器,您可以遍歷服務器中的所有通道,並且在每次迭代中,再次遍歷所有消息。 這將花費很長時間,但沒有其他辦法。

所以你必須把上面的代碼放在另一個循環中,它看起來像這樣:

@client.command()
async def history(ctx, member: discord.Member):
    counter = 0
    for channel in ctx.guild.channels:
        async for message in channel.history(limit = 100):
            if message.author == member:
                counter += 1

    await ctx.send(f'{member.mention} has sent **{counter}** messages in this server.')

暫無
暫無

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

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