【问题标题】:discord.py - How to purge messages from a mentioned user?discord.py - 如何清除提及用户的消息?
【发布时间】:2020-08-10 12:26:56
【问题描述】:

我找到了这个question,但由于某种原因,当我提到用户时它没有找到。当我执行命令时,它不会在终端上显示任何错误。问题中的代码如下所示:

@commands.command()
@commands.has_permissions(manage_messages=True)
async def purge(self, ctx, num: int, 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)

它只适用于q!purge 5 等一些消息,但不适用于q!purge 5 @SomeUser#1234

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    这可以使用Channel.history来完成

    @bot.command()
    async def purge(ctx, limit=50, member: discord.Member=None):
        await ctx.message.delete()
        msg = []
        try:
            limit = int(limit)
        except:
            return await ctx.send("Please pass in an integer as limit")
        if not member:
            await ctx.channel.purge(limit=limit)
            return await ctx.send(f"Purged {limit} messages", delete_after=3)
        async for m in ctx.channel.history():
            if len(msg) == limit:
                break
            if m.author == member:
                msg.append(m)
        await ctx.channel.delete_messages(msg)
        await ctx.send(f"Purged {limit} messages of {member.mention}", delete_after=3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2020-09-07
      • 2019-03-04
      • 2017-10-13
      • 2017-06-30
      • 2018-10-22
      相关资源
      最近更新 更多