【问题标题】:on_message() event only react every x minutes by storing timeon_message() 事件仅通过存储时间每 x 分钟做出反应
【发布时间】:2020-12-07 19:11:38
【问题描述】:

我试图让机器人告诉用户如果他们在特定时间段内发送消息就上床睡觉,但机器人每 x 分钟才做出反应。这是通过检查当前和最后一条消息之间的时间差是否大于 x 来完成的。目前输出什么都没有——甚至没有错误。

我已经设法在“普通”python 中获得了与预期结果相同的结果:

import datetime
import json
now = datetime.datetime.now()
nowHourMin = float( now.hour ) + ( float( now.minute )/60 )   

def get_time():
    filename = "messageRecord.json"
    try:                                 #If file exists, get time of last message
        with open(filename, "r") as f:
            lastMessage = json.load(f)
    except FileNotFoundError:            #If file doesn't exists, create it and store current time
        lastMessage = nowHourMin
        with open(filename, "w") as f:
            json.dump(lastMessage, f)
    return lastMessage

def update_time():                       #Store current time
    filename = "messageRecord.json"
    with open(filename, "w") as f:
        json.dump(nowHourMin, f)           
    
def message_responder():
    lastMessage = get_time()
    dt = nowHourMin - lastMessage
    if (nowHourMin < 23 ) and (dt >0.01):
        print("Please go to bed, it is getting late")
    update_time()

message_responder()

但是当我尝试使代码适应 discord 库时,我没有得到任何结果。没有创建 json 文件,当我自己创建一个文件时,没有任何内容写入文件。它也没有任何错误,根本没有。其他命令和事件工作正常。 这是 discord.py 版本:

import discord
import json
import datetime
from discord.ext import commands, tasks

now = datetime.datetime.now()
nowHourMin = float( now.hour ) + ( float( now.minute )/60 )

async def get_time():
    filename = "messageRecord.json"
    try:                                #If file exists, get time of last message
        with open(filename, "r") as f:
            lastMessage = json.load(f)
    except FileNotFoundError:           #If file doesn't exists, create it and store current time
        lastMessage = nowHourMin
        with open(filename, "w") as f:
            json.dump(lastMessage, f)
    return lastMessage

async def update_time():
    filename = "messageRecord.json"
    with open(filename, "w") as f:
        json.dump(nowHourMin, f)                       

def message_responder(message):
    lastMessage = get_time()                            #Get time of last message from "messageRecord.json"
    dt = nowHourMin - lastMessage                       #Time difference
    if (message.author != client.user) and (nowHourMin < 23 ) and (dt >0.01):   #Values will be adjusted once bot works
        await message.channel.send("Please go to bed, it is getting late")
    else:
        await message.channel.send("Dummy text")        #Will be removed once code works, just to confirm bot is working.
    await update_time()                                 #Updates lastMessage to now

@client.event
async def on_message(message):
    await message_responder(message)
    await client.process_commands(message)              #Prevents on_message()from overwriting commands

【问题讨论】:

    标签: python json discord.py


    【解决方案1】:

    您需要等待get_time(),因为它是一个异步函数。你还需要使message_responder()异步:

    async def message_responder(message): #<- here
        lastMessage = await get_time() #<- and here
        dt = nowHourMin - lastMessage
        if (message.author != client.user) and (nowHourMin < 23 ) and (dt >0.01):
            await message.channel.send("Please go to bed, it is getting late")
        else:
            await message.channel.send("Dummy text")
        await update_time()  
    

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2018-02-18
      相关资源
      最近更新 更多