使用 Discord py 删除来自特定用户的消息

Delete messages from specifc user using Discord py

我想创建一个清除命令,例如 .clear @user#0000 100 它将清除@user#0000 发送的最后 100 条消息。这是我的代码:

    @commands.command()
    @commands.has_permissions(manage_messages=True)
    async def clear(self, ctx, amount=1):
        await ctx.channel.purge(limit=amount + 1)
    @clear.error
    async def clear_error(self, ctx, error):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send('Sorry, you are missing the ``MANAGE_MESSAGES`` permission to use this command.')

我提供的此代码仅删除取决于将在频道中删除多少条消息。我想编写一个代码,让机器人删除来自特定用户的消息。如果可能的话,请告诉我。我会将代码用作将来的参考。非常感谢。

您可以使用 check kwarg 清除来自特定用户的消息:

from typing import Optional 

@commands.command()
@commands.has_permissions(manage_messages=True)
async def clear(self, ctx, member: Optional[discord.Member], amount: int = 1):
    def _check(message):
        if member is None:
            return True
        else:
            if message.author != member:
                return False
            _check.count += 1
            return _check.count <= amount
    _check.count = 0
    await ctx.channel.purge(limit=amount + 1 if member is None else 1000, check=_check)