Я пытаюсь использовать datetime
для вычитания из сохраненного значения datetime
в JSON, чтобы увидеть, прошло ли 2 минуты, прежде чем команда может быть использована снова.Когда я использую команду, каждый раз, когда значение JSON обновляется как «0», даже когда я печатаю currentime
, оно дает мне значение, которое я хочу сохранить.Я пытался изменить timer[id]["flytimer"] = (datetime.datetime.utcnow() - epoch).total_seconds()
на timer[id]["flytimer"] = 0
, но все равно получаю тот же результат
try:
with open("timer.json") as fp:
timer = json.load(fp)
except Exception:
timer = {}
def save_timer():
with open("timer.json", "w+") as fp:
json.dump(timer, fp, sort_keys=True, indent=4)
def add_flytimer(user: discord.User, flytimer: int):
id = user.id
if id not in timer:
timer[id] = {}
timer[id]["flytimer"] = (datetime.datetime.utcnow() - epoch).total_seconds()
save_timer()
def get_flytimer(user: discord.User):
id = user.id
if id in timer:
return timer[id].get("flytimer", 0)
return 0
def reset_flytimer(user: discord.User, flytimer: int):
id = user.id
if id in timer:
timer[id]["flytimer"] = 0
print("{} flight timer reset".format(user.name))
save_timer()
epoch = datetime.datetime.utcfromtimestamp(0)
currenttime = (datetime.datetime.utcnow() - epoch).total_seconds()
if message.content.startswith('!flight'):
print(currenttime)
if get_flytimer is None:
add_flytimer(message.author, currenttime)
print(currenttime)
print(get_flytimer(message.author))
await client.send_message(message.channel, "{} takes a flight to NY".format(message.author))
else:
takeflight = currenttime - get_flytimer(message.author)
if takeflight >= 120:
add_flytimer(message.author,currenttime)
print(get_flytimer(message.author))
await client.send_message(message.channel, "{} takes a flight to NY".format(message.author))
else:
await client.send_message(message.channel, "{} you still have {} seconds until the next flight".format(message.author, takeflight))