-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_functions.py
executable file
·105 lines (87 loc) · 3.81 KB
/
db_functions.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
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
105
import sqlite3
from dataclasses import dataclass
from utils import *
from utils import _reply
@dataclass
class Connection:
session: sqlite3.Connection
cursor: sqlite3.dbapi2.Cursor
def connect():
con = Connection
con.session = sqlite3.connect("database/usernames.db")
con.cursor = con.session.cursor()
return con
# noinspection PyUnboundLocalVariable
def user_in_db(context: CallbackContext, user: variables.UserInfos):
conn = connect()
c = conn.cursor
try:
res = c.execute(
"SELECT username FROM INSCRIPTION_USERS_STATUS WHERE username = '" +
user.username + "'")
except sqlite3.OperationalError:
keyboard = [
[InlineKeyboardButton("⬅ Back to main menu", callback_data="start_over")],
[InlineKeyboardButton("❌ Stop the bot", callback_data="stop")]
]
_reply(user, context, text="Something went wrong while doing the necessary checks with the DB.\n"
"Please contact the staff about this.",
reply_markup=InlineKeyboardMarkup(keyboard))
return None
return res
async def add_user(context: CallbackContext, user: variables.UserInfos):
conn = connect()
c = conn.cursor
keyboard = [
[InlineKeyboardButton("⬅ Back to main menu", callback_data="start_over")],
[InlineKeyboardButton("❌ Stop the bot", callback_data="stop")]
]
# Lo stato è necessariamente quello iniziale (str(INSCRIPTION) = 0)
try:
c.execute(
"INSERT INTO INSCRIPTION_USERS_STATUS VALUES ('" + user.username + "', '0')"
)
except sqlite3.OperationalError:
await _reply(user, context, text="Something went wrong while doing the necessary checks with the DB.\n"
"Please contact the staff about this.",
reply_markup=InlineKeyboardMarkup(keyboard))
conn.session.commit()
conn.session.close()
async def update_subscription_user_status(new_status: str, context: CallbackContext, user: variables.UserInfos):
conn = connect()
c = conn.cursor
keyboard = [
[InlineKeyboardButton("⬅ Back to main menu", callback_data="start_over")],
[InlineKeyboardButton("❌ Stop the bot", callback_data="stop")]
]
try:
c.execute(
"UPDATE INSCRIPTION_USERS_STATUS SET status = '" + new_status +
"' WHERE username = '" + user.username + "'"
)
except sqlite3.OperationalError:
await _reply(user, context, text="Something went wrong while doing the necessary checks with the DB.\n"
"Please contact the staff about this.",
reply_markup=InlineKeyboardMarkup(keyboard))
conn.session.commit()
conn.cursor.close()
# noinspection PyUnboundLocalVariable
async def get_subscription_status(context: CallbackContext, user: variables.UserInfos):
conn = connect()
c = conn.cursor
keyboard = [
[InlineKeyboardButton("⬅ Back to main menu", callback_data="start_over")],
[InlineKeyboardButton("❌ Stop the bot", callback_data="stop")]
]
try:
res = c.execute("SELECT status FROM INSCRIPTION_USERS_STATUS WHERE username = '" + user.username + "'")
except sqlite3.OperationalError:
await _reply(user, context, text="Something went wrong while doing the necessary checks with the DB.\n"
"Please contact the staff about this.",
reply_markup=InlineKeyboardMarkup(keyboard))
res = "err_OperationalError"
if res != "err_OperationalError":
res = res.fetchone()[0]
conn.session.commit()
conn.session.close()
return res