-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
104 lines (82 loc) · 2.94 KB
/
utils.py
File metadata and controls
104 lines (82 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from datetime import datetime
import json
import settings
from typing import Literal
def _get_by_file(file_name: str) -> dict:
"""
:param file_name: .json file name in the data folder
:return: Returns file content as dict
"""
json_files = [file.name for file in settings.DATA_DIR.glob("*.json")]
if file_name not in json_files:
return {}
else:
with open(settings.DATA_DIR / file_name, "r") as file:
file.seek(0)
content = json.load(file)
return content
def get_base_data(local_code) -> dict:
"""
:param local_code: discord.Guild.preferred_locale
:return: Returns the local content of base.json file as dict
"""
base_json = _get_by_file("base.json")
result = base_json.get(str(local_code))
return result if result else base_json["en-US"]
def get_all_guilds() -> dict:
"""
:return: Returns content of guild_info.json as dict
"""
return _get_by_file("guild_info.json")
def get_scheme(scheme_name: Literal['error_scheme', 'guild_scheme']) -> dict:
"""
:param scheme_name: Specified parameter -> ['error_scheme', 'guild_scheme']
:return: Returns the scheme in the schemes.json as dict
"""
schemes = _get_by_file("schemes.json")
return schemes[scheme_name] if scheme_name in schemes else schemes["error_scheme"]
def get_guild_info(guild_id: int) -> dict:
"""
:param guild_id: discord.Guild.id
:return: Returns guild data in the guild_info.json as dict by id.
"""
guilds = get_all_guilds()
return guilds[str(guild_id)] if str(guild_id) in guilds else {}
def update_guild(guild_dict: dict, remove: bool = False) -> dict:
"""
:param guild_dict: The data that shaped by structure {<guild_id>: <guild_scheme>}
:param remove: If remove is equals to True, removes the guild data from guild_info.json
:return: Returns updated guilds.json as dict
"""
guild_id = str(list(guild_dict.keys())[0])
guilds = get_all_guilds()
if remove and guild_id in guilds:
guilds.pop(guild_id)
else:
guilds.update(guild_dict)
with open(settings.DATA_DIR / "guild_info.json", "w") as file:
file.seek(0)
json.dump(guilds, file, indent=4, )
return guilds
def add_guild(guild_id: int) -> dict:
"""
Builds the guild structure and updates the guild_info.json
:param guild_id: discord.Guild.id
:return: Returns builded structure as dict
"""
guild_dict = {str(guild_id): get_scheme("guild_scheme")}
update_guild(guild_dict)
return guild_dict
def remove_guild(guild_id: int) -> None:
"""
:param guild_id: discord.Guild.id
:return: None
"""
update_guild({str(guild_id): {}}, remove=True)
def funny_log(line: str) -> None:
"""
:param line: The data to be added to funny.log file
:return: None
"""
with open(settings.BASE_DIR / "logs" / "funny.log", "a") as file:
file.write(line + str(datetime.now()) + "\n")