discord.py-如何清除提到的用户的消息?

时间:2020-08-10 04:14:42

标签: python discord.py

我找到了这个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

1 个答案:

答案 0 :(得分: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)