Python 是否有办法检查固定邮件,并使用discord.py仅清除特定成员的邮件?

Python 是否有办法检查固定邮件,并使用discord.py仅清除特定成员的邮件?,python,discord.py,discord.py-rewrite,Python,Discord.py,Discord.py Rewrite,我想制作一个类似于Dyne的清除命令,在这里您可以输入一个用户,而它不清除用户的消息,只清除用户的消息(如果您输入一个用户)。我试过做一个单独的检查功能,但它不能清除任何东西。我没有错误,它只是不会清除 @commands.command() @commands.has_permissions(manage_messages=True) async def purge(self, ctx, user: discord.Member = None, num: int = 10000

我想制作一个类似于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正常工作吗?

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

@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\u after
,它将在给定的秒数后自动删除消息。还有一些我没有提到的其他语法问题,比如check函数只接受一个参数,但解析了两个参数,我也修复了这两个参数。从技术上讲,PEP8禁止在变量中存储lambda,但我认为这是可以原谅的


编辑:你可以做一些事情,如检查
num==“*”
并删除所有消息(如果有)。

我所做的更改应该可以解决你的问题,也可以处理一些其他问题

@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\u after
,它将在给定的秒数后自动删除消息。还有一些我没有提到的其他语法问题,比如check函数只接受一个参数,但解析了两个参数,我也修复了这两个参数。从技术上讲,PEP8禁止在变量中存储lambda,但我认为这是可以原谅的

编辑:您可以执行类似于检查
num==“*”
的操作,并删除所有消息