2023-03-30 17:01:04 +00:00
|
|
|
import json
|
|
|
|
import os.path
|
2023-04-05 10:10:18 +00:00
|
|
|
import os
|
2023-03-30 17:01:04 +00:00
|
|
|
|
2024-03-02 08:56:41 +00:00
|
|
|
from robocop_ng.helpers.data_loader import read_json
|
2023-10-09 20:56:13 +00:00
|
|
|
|
2023-03-30 17:01:04 +00:00
|
|
|
|
2023-04-05 10:10:18 +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
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
|
|
|
|
def get_persistent_roles(bot) -> dict[str, list[str]]:
|
2024-03-02 08:56:41 +00:00
|
|
|
return read_json(bot, get_persistent_roles_path(bot))
|
2023-03-30 17:01:04 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +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)
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
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]
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
persistent_roles = get_persistent_roles(bot)
|
2023-03-30 17:01:04 +00:00
|
|
|
persistent_roles[uid] = roles
|
2023-04-05 10:10:18 +00:00
|
|
|
set_persistent_roles(bot, persistent_roles)
|
2023-03-30 17:01:04 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def get_user_roles(bot, uid: int) -> list[str]:
|
2023-03-30 17:01:04 +00:00
|
|
|
uid = str(uid)
|
2024-03-02 08:56:41 +00:00
|
|
|
persistent_roles = get_persistent_roles(bot)
|
|
|
|
return persistent_roles[uid] if uid in persistent_roles else []
|