且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止用户在频道中发送多条消息?

更新时间:2022-06-19 07:04:12

您可以使用 on_message() 事件并将频道历史记录的限制设置为 2:

You can use the on_message() event and set the channel history's limit to 2 for this:

@bot.event
async def on_message(message):
    recent_author = (await message.channel.history(limit=2).flatten())[1].author
    if message.author == recent_author:
        await message.delete()

history() 协程首先获取最新消息,除非另有说明,因此您可以将限制设置为 2 以获取用户刚刚发送的消息之前的最新消息.

The history() coroutine gets the newest messages first, unless specified otherwise, so you can set the limit to 2 to get the most recent message before the one the user just sent.

参考资料: