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
|
|
|
|
|
|
|
|
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]]:
|
|
|
|
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 {}
|
|
|
|
|
|
|
|
|
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)
|
2023-04-05 10:10:18 +00:00
|
|
|
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 []
|