首页 > 解决方案 > 如何在 discord.py 的“commands.Bucket”中获取限速人员

问题描述

所以我有一个反垃圾邮件系统,它是在on_messagediscord.py 事件中制作的。相同的代码如下-->

@bot.event
async def on_message(message):
    await bot.wait_until_ready()
    if message.author == bot.user:
        return
    bucket = bot.AntiSpam.get_bucket(message)
    retry_after = bucket.update_rate_limit()
    if retry_after:
        print('Someone was being naughty')
        await message.channel.purge(before=datetime.utcnow(), after=datetime.utcnow()-timedelta(seconds=5))
        await message.channel.send(f'Please don\'t spam, {message.author.mention}', delete_after=5.0)

这将删除自最后 5 秒以来发送的每条消息。如何使其删除该垃圾邮件发送者发送的消息?我知道我必须使用checkkwarg of TextChannel.purge(),但我需要一种方法来进行检查。我能得到任何帮助吗?

标签: pythonpython-3.xdiscord.py

解决方案


检查将非常简单:

def check(m):
    return m.author == message.author

await message.channel.purge(before=datetime.utcnow(), after=datetime.utcnow()-timedelta(seconds=5), check=check)

我们正在检查传递给检查的消息的作者是否与上一条消息的作者相同。


推荐阅读