ryuko-ng/robocop_ng/helpers/roles.py
TSRBerry 9669556a39
Handle various file related issues (#76)
* Create state files if they don't exist yet

* Add notifications helper to message bot managers

* Inform bot managers about errors if possible

* Handle JSONDecodeErrors including empty files
2023-10-09 22:56:13 +02:00

50 lines
1.4 KiB
Python

import json
import os.path
import os
from robocop_ng.helpers.notifications import report_critical_error
def get_persistent_roles_path(bot):
return os.path.join(bot.state_dir, "data/persistent_roles.json")
def get_persistent_roles(bot) -> dict[str, list[str]]:
if os.path.isfile(get_persistent_roles_path(bot)):
with open(get_persistent_roles_path(bot), "r") as f:
try:
return json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}
def set_persistent_roles(bot, contents: dict[str, list[str]]):
with open(get_persistent_roles_path(bot), "w") as f:
json.dump(contents, f)
def add_user_roles(bot, uid: int, roles: list[int]):
uid = str(uid)
roles = [str(x) for x in roles]
persistent_roles = get_persistent_roles(bot)
persistent_roles[uid] = roles
set_persistent_roles(bot, persistent_roles)
def get_user_roles(bot, uid: int) -> list[str]:
uid = str(uid)
with open(get_persistent_roles_path(bot), "r") as f:
roles = json.load(f)
if uid in roles:
return roles[uid]
return []