ryuko-ng/robocop_ng/helpers/roles.py

38 lines
986 B
Python
Raw Normal View History

2023-03-30 17:01:04 +00:00
import json
import os.path
import os
2023-03-30 17:01:04 +00:00
def get_persistent_roles_path(bot):
return os.path.join(bot.state_dir, "data/persistent_roles.json")
2023-03-30 17:01:04 +00:00
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:
2023-03-30 17:01:04 +00:00
return json.load(f)
return {}
def set_persistent_roles(bot, contents: dict[str, list[str]]):
with open(get_persistent_roles_path(bot), "w") as f:
2023-03-30 17:01:04 +00:00
json.dump(contents, f)
def add_user_roles(bot, uid: int, roles: list[int]):
2023-03-30 17:01:04 +00:00
uid = str(uid)
roles = [str(x) for x in roles]
persistent_roles = get_persistent_roles(bot)
2023-03-30 17:01:04 +00:00
persistent_roles[uid] = roles
set_persistent_roles(bot, persistent_roles)
2023-03-30 17:01:04 +00:00
def get_user_roles(bot, uid: int) -> list[str]:
2023-03-30 17:01:04 +00:00
uid = str(uid)
with open(get_persistent_roles_path(bot), "r") as f:
2023-03-30 17:01:04 +00:00
roles = json.load(f)
if uid in roles:
return roles[uid]
return []