blob: e1ce6b7a7456103489bb4b1948fc99f81f3a1835 (
plain)
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
|
import json
import aiofiles
from typing import Optional
from ATRI.exceptions import WriteError
from . import SERVICE_DIR
class BanSystem:
file_name = "banlist.service.json"
path = SERVICE_DIR / file_name
path.parent.mkdir(exist_ok=True, parents=True)
try:
data = json.loads(path.read_bytes())
except:
data = {}
@classmethod
def get_list(cls) -> dict:
return cls.data
@classmethod
def is_in_list(cls, user: Optional[str]) -> bool:
return False if user in cls.data else True
@classmethod
async def add_to_list(cls, user: Optional[str]) -> None:
cls.data[user] = user
try:
async with aiofiles.open(
cls.path, 'w', encoding='utf-8') as target:
await target.write(
json.dumps(
cls.data, indent=4
)
)
except WriteError:
raise WriteError("Writing file failed!")
@classmethod
async def del_from_list(cls, user: Optional[str]) -> None:
del cls.data[user]
try:
async with aiofiles.open(
cls.path, 'w', encoding='utf-8') as target:
await target.write(
json.dumps(
cls.data, indent=4
)
)
except WriteError:
raise WriteError("Writing file failed!")
|