python - (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
但是目前它会回复该时间段内的每条消息,这很烦人。我知道命令有冷却时间,但我无法为事件找到类似的东西。
所以我的问题是:是否有相当于事件的冷却时间,或另一种聪明的方式?
在此先感谢,并为我应该犯的新手错误感到抱歉。
解决方案
是的,聪明的方法是使用数据库来存储用户信息和他们发送的最后一条消息。然后你计算保存到当前时间的时间,如果保存的时间超过了一定的时间,就告诉他们去睡觉。
推荐阅读
- java - 如何在 Visual Studio 代码中调试 Java 流?
- sql - 在 postgresql 上查询 Json 字段
- javascript - 当有多个文件类型时,材质 UI 输入不起作用
- iphone - 是否可以通过 GitHub iphone 应用程序分叉存储库?
- visual-studio-code - VSCode上下移动多行选择
- mysql - 带有 mysql 的 Spring Boot 应用程序卡在“Hikari-Pool-1 - 正在启动...”
- elasticsearch - Elasticsearch:自定义分析器中 synonym_graph 和停止过滤器之间的意外交互
- ssl - 如何使用 scapy 解密从 pcap 文件读取的 TLS 流量?
- salesforce-marketing-cloud - 我正在尝试过滤所有订阅者中的订阅者,以识别通过创建配置文件属性从哪个 MID 创建订阅者
- java - Java sftpChannel put error (系统找不到指定的路径)