blob: 06cc381bcc4121cfa9492be24e8e9223553e53d5 (
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 InvalidWriteText
from . import SERVICE_PATH
class BanList:
filename = 'banlist.service.json'
path = SERVICE_PATH / filename
path.parent.mkdir(exist_ok=True, parents=True)
try:
data = json.loads(path.read_bytes())
except:
data = {}
@classmethod
def get_banlist(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_list(cls, user: Optional[str]) -> None:
try:
cls.data[user] = user
async with aiofiles.open(
cls.path, 'w', encoding='utf-8') as target:
await target.write(
json.dumps(
cls.data, indent=4
)
)
except InvalidWriteText:
raise InvalidWriteText('Writing file failed!')
@classmethod
async def del_list(cls, user: Optional[str]) -> None:
try:
del cls.data[user]
async with aiofiles.open(
cls.path, 'w', encoding='utf-8') as target:
await target.write(
json.dumps(
cls.data, indent=4
)
)
except InvalidWriteText:
raise InvalidWriteText('List writing file failed!')
|