首页 > 解决方案 > (discord.py) 让 Bot 每 x 秒只对 on_message() 做出反应

问题描述


我想让我的 Discord 机器人能够告诉用户在他们迟到时去睡觉,我正在这样做:

@client.event
async def on_message(message):
    now = datetime.datetime.now()
    now_hour = now.hour                                      #Gets current time in hours.
    if message.author != client.user and now_hour<6:        #Runs if the message is not sent by the bot or if the time is between 00:00 and 06:00
        await message.channel.send('Are you still awake? You ought to go to bed immediately!')
    await client.process_commands(message)                   #If not included, on_message() will override commands

但是目前它会回复该时间段内的每条消息,这很烦人。我知道命令有冷却时间,但我无法为事件找到类似的东西。
所以我的问题是:是否有相当于事件的冷却时间,或另一种聪明的方式?

在此先感谢,并为我应该犯的新手错误感到抱歉。

标签: pythondiscord.py

解决方案


是的,聪明的方法是使用数据库来存储用户信息和他们发送的最后一条消息。然后你计算保存到当前时间的时间,如果保存的时间超过了一定的时间,就告诉他们去睡觉。


推荐阅读