-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
71 lines (53 loc) · 2.46 KB
/
database.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
66
67
68
69
70
71
import disnake
import aiosqlite
class UserDataBase:
def __init__(self):
self.name = 'dbs/users.db'
async def create_table(self):
async with aiosqlite.connect(self.name) as db:
cursor = await db.cursor()
query = '''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY key,
name TEXT,
warns INTEGER,
roles TEXT
);'''
await cursor.execute(query)
await db.commit()
async def add_warns(self, user: disnake.Member, value = 1):
async with aiosqlite.connect(self.name) as db:
cursor = await db.cursor()
query = 'UPDATE users SET warns = warns +? WHERE id = ?'
await cursor.execute(query, (value,user.id))
await db.commit()
async def remove_warns(self, user: disnake.Member, value = 1):
async with aiosqlite.connect(self.name) as db:
cursor = await db.cursor()
query = 'UPDATE users SET warns = warns -? WHERE id = ?'
await cursor.execute(query, (value,user.id))
await db.commit()
async def add_user(self, user: disnake.Member):
async with aiosqlite.connect(self.name) as db:
if not await self.get_user(user):
cursor = await db.cursor()
query = 'INSERT INTO users (id, name, warns, roles) VALUES (?, ?, ?, ?);'
await cursor.execute(query, (user.id, user.name, 0, ''))
await db.commit()
async def get_user(self, user: disnake.Member):
async with aiosqlite.connect(self.name) as db:
cursor = await db.cursor()
query = 'SELECT * FROM users WHERE id = ?'
await cursor.execute(query, (user.id,))
return await cursor.fetchone()
async def add_roles(self, user: disnake.Member, roles):
async with aiosqlite.connect(self.name) as db:
cursor = await db.cursor()
query = 'UPDATE users SET roles = ? WHERE id = ?'
await cursor.execute(query, (roles,user.id))
await db.commit()
async def remove_roles(self, user: disnake.Member):
async with aiosqlite.connect(self.name) as db:
cursor = await db.cursor()
query = 'UPDATE users SET roles = ? WHERE id = ?'
await cursor.execute(query, (1,user.id))
await db.commit()