简体   繁体   English

有没有办法检查固定消息,并且只使用 discord.py 清除某些成员消息?

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

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).我想制作一个类似于 Dyne 的清除命令,您可以在其中输入用户,并且它不会清除固定的,只有用户的消息(如果您输入用户)。 I've tried making a seperate check function, but it doesn't purge anything.我试过单独检查 function,但它没有清除任何东西。 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()

I have manage messages permissions on the server.我有管理服务器上的消息权限。 Does anyone know how to make the check_func work properly?有谁知道如何使 check_func 正常工作?

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)

Just changing the function based on the arguments looks better and is more efficient as otherwise you have duplicated code.只需基于 arguments 更改 function 看起来更好,效率更高,否则您有重复的代码。 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. channel.send有一个参数delete_after它将在给定的秒数后自动删除一条消息。 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.还有一些我没有提到的其他语法问题,例如检查 function 只接受一个参数,但您解析两个我也已修复。 Technically PEP8 prohibits storing lambda's in variables but I think this could be excused.从技术上讲,PEP8 禁止将 lambda 存储在变量中,但我认为这可以原谅。

Edit : you could do something like check if num == "*" and delete all messages if it does.编辑:您可以执行类似检查是否num == "*"并删除所有消息的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM