Discord/modules/utils_sync.py

46 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-05-08 17:39:46 +03:00
from typing import Any, Union
2023-05-04 17:09:47 +03:00
2023-05-08 17:39:46 +03:00
from discord import Member, User
2023-05-04 17:09:47 +03:00
from ujson import dumps, loads
def json_read_sync(path: str) -> Any:
with open(path, mode="r", encoding="utf-8") as f:
data = f.read()
return loads(data)
def json_write_sync(data: Any, path: str) -> None:
with open(path, mode="w", encoding="utf-8") as f:
2023-05-06 18:51:07 +03:00
f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
2023-05-04 17:09:47 +03:00
def config_get_sync(key: str, *path: str) -> Any:
this_key = json_read_sync("config.json")
for dict_key in path:
this_key = this_key[dict_key]
return this_key[key]
def config_set_sync(key: str, value: Any, *path: str) -> None:
this_dict = json_read_sync("config.json")
string = "this_dict"
for arg in path:
string += f'["{arg}"]'
if type(value) in [str]:
string += f'["{key}"] = "{value}"'
else:
string += f'["{key}"] = {value}'
exec(string)
json_write_sync(this_dict, "config.json")
return
2023-05-04 17:14:23 +03:00
2023-05-08 17:39:46 +03:00
def guild_name(member: Union[Member, User]) -> str:
if isinstance(member, User):
return member.name
2023-05-04 17:09:47 +03:00
if member.nick == None:
return member.name
else:
2023-05-04 17:14:23 +03:00
return member.nick