2018-12-27 21:36:18 +00:00
|
|
|
import json
|
|
|
|
import math
|
2023-04-05 10:10:18 +00:00
|
|
|
import os
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def get_crontab_path(bot):
|
|
|
|
return os.path.join(bot.state_dir, "data/robocronptab.json")
|
|
|
|
|
|
|
|
|
|
|
|
def get_crontab(bot):
|
|
|
|
with open(get_crontab_path(bot), "r") as f:
|
2018-12-27 21:36:18 +00:00
|
|
|
return json.load(f)
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def set_crontab(bot, contents):
|
|
|
|
with open(get_crontab_path(bot), "w") as f:
|
2018-12-27 21:36:18 +00:00
|
|
|
f.write(contents)
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def add_job(bot, job_type, job_name, job_details, timestamp):
|
2018-12-27 21:36:18 +00:00
|
|
|
timestamp = str(math.floor(timestamp))
|
|
|
|
job_name = str(job_name)
|
2023-04-05 10:10:18 +00:00
|
|
|
ctab = get_crontab(bot)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
if job_type not in ctab:
|
|
|
|
ctab[job_type] = {}
|
|
|
|
|
|
|
|
if timestamp not in ctab[job_type]:
|
|
|
|
ctab[job_type][timestamp] = {}
|
|
|
|
|
|
|
|
ctab[job_type][timestamp][job_name] = job_details
|
2023-04-05 10:10:18 +00:00
|
|
|
set_crontab(bot, json.dumps(ctab))
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def delete_job(bot, timestamp, job_type, job_name):
|
2018-12-27 21:36:18 +00:00
|
|
|
timestamp = str(timestamp)
|
|
|
|
job_name = str(job_name)
|
2023-04-05 10:10:18 +00:00
|
|
|
ctab = get_crontab(bot)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
del ctab[job_type][timestamp][job_name]
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
set_crontab(bot, json.dumps(ctab))
|