【问题标题】:Delete messages from specifc user using Discord py使用 Discord py 删除特定用户的消息
【发布时间】:2022-01-20 20:52:56
【问题描述】:

我想创建一个清除命令,例如 .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.')

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

【问题讨论】:

    标签: discord discord.py message


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 我想做一个正常的清除命令,其中 .clear 清除最后一条消息, .clear @user#0000 清除用户的最后一条消息,我得到了你的代码,但是否有可能实现这两种功能到一个命令?
    • 可以理解,我编辑了我的答案。请检查并尝试一下。
    • 我发现,当我在没有提及任何用户的情况下执行 .clear 命令时,它确实可以正常清除消息。当我执行 .clear @user#0000 5, 5 时,然后检查最后发送的 5 条消息,并且只删除该范围内的 @user#0000 的消息。
    • 附加信息,我编辑了限制数量(limit=amount+1)也删除了我输入命令的消息,所以最后发送的消息范围不包括我发送的命令。
    • 好的,再次查看答案。我编辑了它。
    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 2019-06-16
    • 2021-11-11
    • 2020-09-13
    • 2020-10-01
    • 2021-10-18
    • 2020-10-01
    • 2018-09-07
    相关资源
    最近更新 更多