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

标签 discord discord.py message

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

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

最佳答案

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

关于discord - 使用 Discord py 删除来自特定用户的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70401484/

相关文章:

javascript - 是否可以通过发送消息来打开 Discord.js 机器人?

javascript - 使用 javascript 的 Discord 机器人 .sendMessage 不是函数

python - Discord.py 循环播放音频源

响应消息的 WCF charset=utf-8 与绑定(bind)的内容类型不匹配 (application/soap+xml; charset=utf-8)

validation - Clojure 中带有错误处理的管道

python-3.x - 如何使用discord.py 让discord 机器人在其自己的消息上触发命令?

javascript - 如何从discord.js 事件中检索用户信息?

python - 从消息中提取用户名模式

python - 如何使用线程同时运行不和谐客户端和 pygame?

python - 电子邮件正文有时是一个字符串,有时是一个列表。为什么?