首页 > 解决方案 > 有没有办法检查固定的消息,并且只使用 discord.py 清除某些成员的消息?

问题描述

我想制作一个类似于 Dyne 的清除命令,您可以在其中输入用户,并且它不会清除固定的,只有用户的消息(如果您输入用户)。我试过做一个单独的检查功能,但它不会清除任何东西。我没有错误,它只是不会清除。

@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 正常工作?

标签: pythondiscord.pydiscord.py-rewrite

解决方案


我所做的更改应该可以解决您的问题以及处理其他一些问题。

@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)

仅根据参数更改函数看起来更好,效率更高,否则您会重复代码。此外,您最后发送的消息立即被有效删除。channel.send有一个参数delete_after会在给定的秒数后自动删除一条消息。还有一些我没有提到的其他语法问题,比如 check 函数只接受一个参数,但你解析了两个我也修复了。从技术上讲,PEP8 禁止将 lambda 存储在变量中,但我认为这可以原谅。

编辑:你可以做一些检查,如果是num == "*",则删除所有消息。


推荐阅读