簡體   English   中英

有沒有辦法檢查固定消息,並且只使用 discord.py 清除某些成員消息?

[英]Is there a way to do a check for pinned messages, and only purge a certain members messages using discord.py?

我想制作一個類似於 Dyne 的清除命令,您可以在其中輸入用戶,並且它不會清除固定的,只有用戶的消息(如果您輸入用戶)。 我試過單獨檢查 function,但它沒有清除任何東西。 我沒有錯誤,它只是不會清除。

@commands.command()
    @commands.has_permissions(manage_messages=True)
    async def purge(self, ctx, user: discord.Member = None, num: int = 10000):
        if user:
            def check_func(user: discord.Member, message: discord.Message):
                return not msg.pinned
                return user.id
            await ctx.message.delete()
            await ctx.channel.purge(limit=num, check=check_func)
            verycool = await ctx.send(f'{num} messages deleted.')
            await verycool.delete()

        else:
            await ctx.message.delete()
            await ctx.channel.purge(limit=num, check=lambda msg: not msg.pinned)
            verycool = await ctx.send(f'{num} messages deleted.')
            await verycool.delete()

我有管理服務器上的消息權限。 有誰知道如何使 check_func 正常工作?

我所做的更改應該可以解決您的問題以及處理其他一些問題。

@commands.command()
@commands.has_permissions(manage_messages=True)
async def purge(self, ctx, num: int = None, user: discord.Member = None):
    if user:
        check_func = lambda msg: msg.author == user and not msg.pinned
    else:
        check_func = lambda msg: not msg.pinned

    await ctx.message.delete()
    await ctx.channel.purge(limit=num, check=check_func)
    await ctx.send(f'{num} messages deleted.', delete_after=5)

只需基於 arguments 更改 function 看起來更好,效率更高,否則您有重復的代碼。 此外,您最后發送的消息立即被有效刪除。 channel.send有一個參數delete_after它將在給定的秒數后自動刪除一條消息。 還有一些我沒有提到的其他語法問題,例如檢查 function 只接受一個參數,但您解析兩個我也已修復。 從技術上講,PEP8 禁止將 lambda 存儲在變量中,但我認為這可以原諒。

編輯:您可以執行類似檢查是否num == "*"並刪除所有消息的操作。

暫無
暫無

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

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