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

2024-09-27 21:34:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作一个类似于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()

我在服务器上具有“管理邮件”权限。有人知道如何使检查功能正常工作吗


Tags: 用户功能消息messagecheckmsgawaitdelete
1条回答
网友
1楼 · 发布于 2024-09-27 21:34:27

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

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

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

相关问题 更多 >

    热门问题