有没有一种方法可以检查固定的邮件,并且仅使用discord.py清除某些成员的邮件? [英] Is there a way to do a check for pinned messages, and only purge a certain members messages using discord.py?

查看:35
本文介绍了有没有一种方法可以检查固定的邮件,并且仅使用discord.py清除某些成员的邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a purge command similar to Dyne's, where you can input a user, and it doesn't purge pinned, only the user's messages (if you input a user). I've tried making a seperate check function, but it doesn't purge anything. I get no error, it just won't purge.

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

I have manage messages permissions on the server. Does anyone know how to make the check_func work properly?

推荐答案

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

The changes I have made should solve your issue as well as deal with a few other issues.

@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存储在变量中,但我认为可以原谅.

Just changing the function based on the arguments looks better and is more efficient as otherwise you have duplicated code. Furthermore, the message you send at the end was being deleted effectively immediately. channel.send has an argument delete_after which will automatically deletes a message after a given amount of seconds. There are a few other syntax issue I haven't mentioned such as the check function only taking one argument but you parsing two which I have also fixed. Technically PEP8 prohibits storing lambda's in variables but I think this could be excused.

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

Edit: you could do something like check if num == "*" and delete all messages if it does.

这篇关于有没有一种方法可以检查固定的邮件,并且仅使用discord.py清除某些成员的邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆