-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbOperator.py
191 lines (171 loc) · 6.22 KB
/
dbOperator.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import sqlite3
class DbOperator():
connection = sqlite3.connect('subs.db')
cursor = connection.cursor()
query = 'select sqlite_version();'
cursor.execute(query)
log_file = "logs.txt"
result = cursor.fetchall()
hash_dict = {}
print("SQLite initiliazed. Version is : {}".format(result))
table = ''' CREATE TABLE IF NOT EXISTS subs(
id INTEGER PRIMARY KEY ,
chat_id varchar(30) NOT NULL,
user_id varchar(30) NOT NULL,
username varchar(50) NOT NULL,
menu_sub BOOLEAN DEFAULT FALSE,
sub_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_staff BOOLEAN DEFAULT FALSE
);
'''
cursor.execute(table)
connection.commit()
def save_log(self,text):
with open("{}".format(self.log_file), "a") as file:
file.write(f"\n{text}")
print(f"LOG FILE INFO : {text}")
def dict_ekle(self,hash1,text):
self.hash_dict[hash1] = text
def dict_cek(self,hash):
toreturn = self.hash_dict.get(hash)
self.hash_dict.clear()
return toreturn
def addSub(self,sender):
username = sender.username
chat_id = sender.id
if not username:
username = "Unknown"
userId = sender.id
cursor = self.cursor
current_subs = self.getSubs()
check = self.checkSub(userId)
if check:
return False
else:
if current_subs:
execute = f'''INSERT INTO subs (chat_id, user_id, username) VALUES ('{chat_id}','{userId}','{username}')'''
else:
execute = f'''INSERT INTO subs (chat_id, user_id, username, is_staff) VALUES ('{chat_id}','{userId}','{username}',TRUE)'''
cursor.execute(execute)
self.connection.commit()
return True
def deleteSub(self,sender):
cursor = self.cursor
userId = sender.id
check = self.checkSub(userId)
if check:
statement = '''DELETE FROM subs WHERE user_id = ?'''
cursor.execute(statement, (userId,))
self.connection.commit()
return True
else:
return False
def checkSub(self,userid):
cursor = self.cursor
statement = '''SELECT username FROM subs WHERE user_id = ?'''
cursor.execute(statement,(userid,))
output = cursor.fetchone()
if output:
return True
else:
return False
def checkMenuSub(self,userid):
cursor = self.cursor
statement = '''SELECT menu_sub FROM subs WHERE user_id = ?'''
cursor.execute(statement,(userid,))
output = cursor.fetchone()[0]
if output == 1:
return True
else:
return False
def getSubs(self):
cursor = self.cursor
subs_id = []
statement = '''SELECT user_id FROM subs'''
cursor.execute(statement)
output = cursor.fetchall()
if output:
for row in output:
subs_id.append(row[0])
return subs_id
def getMenuSubs(self):
cursor = self.cursor
subs_id = []
statement = '''SELECT user_id FROM subs WHERE menu_sub = TRUE'''
cursor.execute(statement)
output = cursor.fetchall()
if output:
for row in output:
subs_id.append(row[0])
return subs_id
def getAdmins(self):
cursor = self.cursor
admins_id = []
statement = '''SELECT user_id FROM subs WHERE is_staff = TRUE'''
cursor.execute(statement)
output = cursor.fetchall()
if output:
for row in output:
admins_id.append(row[0])
return admins_id
def addStaff(self,arg_id):
cursor = self.cursor
check= self.checkSub(arg_id)
if check:
checkStaff = self.checkStaff(arg_id)
if checkStaff == 1:
return "Kullanici zaten admin.",False
statement = '''UPDATE subs SET is_staff = TRUE WHERE user_id = ?;'''
cursor.execute(statement,(arg_id,))
self.connection.commit()
return f"{arg_id} kodlu kullanici basariyla admin yapildi.",True
else:
return "Kullanici abone degil ya da kullanici idsi yanlış.",False
def addMenuSub(self,userid):
cursor = self.cursor
if self.checkMenuSub(userid=userid):
return False
try:
statement = '''UPDATE subs SET menu_sub = TRUE where user_id = ?'''
cursor.execute(statement,(userid,))
self.connection.commit()
return True
except:
return False
def leaveMenuSub(self,userid):
cursor = self.cursor
if not self.checkMenuSub(userid=userid):
return False
try:
statement = '''UPDATE subs SET menu_sub = FALSE where user_id = ?'''
cursor.execute(statement,(userid,))
self.connection.commit()
return True
except:
return False
def deleteStaff(self,arg_id):
cursor = self.cursor
check= self.checkSub(arg_id)
if check:
check = self.checkStaff(arg_id)
if check == 0:
return "Kullanici admin değil.",False
statement = '''UPDATE subs SET is_staff = FALSE WHERE user_id = ?;'''
cursor.execute(statement,(arg_id,))
self.connection.commit()
return f"{arg_id} kodlu kullanicinin adminliği alındı.",True
else:
return "Kullanici abone degil ya da kullanici adı yanlış.",False
def checkStaff(self,user_id):
cursor = self.cursor
if self.checkSub(user_id):
statement = '''SELECT is_staff FROM subs WHERE user_id = ?'''
cursor.execute(statement,(user_id,))
output = cursor.fetchone()[0]
return output
return 2
def getId(self,username):
cursor = self.cursor
statement = '''SELECT user_id FROM subs WHERE username = ?'''
cursor.execute(statement,(username,))
return cursor.fetchone()