【问题标题】:Is there a way to do a check for pinned messages, and only purge a certain members messages using discord.py?有没有办法检查固定的消息,并且只使用 discord.py 清除某些成员的消息?
【发布时间】:2020-09-25 05:24:19
【问题描述】:

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

【问题讨论】:

    标签: python discord.py discord.py-rewrite


    【解决方案1】:

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

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

    编辑:您可以执行类似检查num == "*" 的操作,如果是则删除所有消息。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 1970-01-01
      • 2021-09-14
      • 2020-10-17
      • 2020-12-03
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多