ryuko-ng/robocop_ng/helpers/roles.py

34 lines
894 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
from robocop_ng.helpers.data_loader import read_json
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]]:
return read_json(bot, get_persistent_roles_path(bot))
2023-03-30 17:01:04 +00:00
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)
persistent_roles = get_persistent_roles(bot)
return persistent_roles[uid] if uid in persistent_roles else []