forked from nh-server/Kurisu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
65 lines (55 loc) · 2.11 KB
/
manager.py
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
import aiosqlite3
import json
class Manager:
def __init__(self, bot):
self.dbcon = bot.holder
@staticmethod
def format_args(args: dict) -> str:
if len(args) < 1:
return ''
statement = 'WHERE '
conditions = []
for kword, value in args.items():
conditions.append(f"{kword} = '{value}'")
return statement + ' AND '.join(conditions)
class WordFilterManager(Manager):
def __init__(self, bot):
super().__init__(bot)
self.kinds = ('piracy tool', 'piracy video', 'piracy tool alert', 'drama', 'unbanning tool', 'piracy site')
self.filter = {}
async def load(self):
for kind in self.kinds:
self.filter[kind] = [i[0] for i in await self.fetch(kind=kind)]
print("Loaded word filter")
async def bulk_load(self):
with open("wordfilter.json", 'r') as f:
dict = json.load(f)
values = []
for kind in self.kinds:
if kind in dict:
for entry in dict[kind]:
values.append(f"('{entry}','{kind}')")
async with self.dbcon as cur:
await cur.execute(f"INSERT INTO wordfilter VALUES {','.join(values)}")
await self.load()
async def add(self, word: str, kind: str) -> tuple:
async with self.dbcon as cur:
try:
await cur.execute(f'INSERT INTO wordfilter VALUES ("{word}","{kind}")')
except aiosqlite3.IntegrityError as e:
print(e)
return None, None
await self.load()
return word, kind
async def fetch(self, **kwargs) -> tuple:
cond = self.format_args(kwargs)
async with self.dbcon as cur:
await cur.execute(f'SELECT word FROM wordfilter {cond}')
return await cur.fetchall()
async def delete(self, word: str):
if not await self.fetch(word=word):
return None
async with self.dbcon as cur:
await cur.execute(f'DELETE FROM wordfilter WHERE word="{word}"')
await self.load()
return word