-
Notifications
You must be signed in to change notification settings - Fork 5
/
dbmanager.py
316 lines (283 loc) · 13 KB
/
dbmanager.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import datetime
import pymysql, hashlib
def getError(exception):
return str(exception).replace('(', '').replace(')', '').split(',')[0]
class dbm:
def __init__(self, username, password, host, db_name):
self.username, self.password, self.host, self.db_name = username, password, host, db_name
def connect(self):
try:
self.connection = None
self.connection = pymysql.connect(
host=self.host,
port=3306,
user=self.username,
password=self.password,
database=self.db_name,
cursorclass=pymysql.cursors.DictCursor
)
return bool(self.connection)
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
def close(self):
try:
self.connection.close()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, ex]
def registered(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select username from `users` where id=%s""", (discordID,))
return [True, False if cursor.fetchone() is None else True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def getUsernameByDiscordID(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""SELECT username FROM `users` WHERE id = %s""", (discordID,))
return [True, cursor.fetchone()]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def register(self, discordID, username, password, birthday):
try:
password = hashlib.sha256(password.encode('utf-8')).hexdigest()
data = datetime.date.today() + datetime.timedelta(days=7)
with self.connection.cursor() as cursor:
cursor.execute(
"""insert into `users` (id, username, password, birthday) values (%s, %s, %s, %s)""", (discordID, username, password, birthday,))
cursor.execute("""insert into `store` (id, data_trial) values (%s, %s)""", (discordID, data,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def changePassword(self, discordID, password):
try:
password = hashlib.sha256(password.encode('utf-8')).hexdigest()
with self.connection.cursor() as cursor:
cursor.execute("""update `users` set password = %s where id=%s""", (password, discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def changeUsername(self, discordID, username):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select username from `users` where id=%s""", (discordID,))
name = cursor.fetchone()
cursor.execute("""update `users` set username = %s where id=%s""", (username, discordID,))
self.connection.commit()
return [True, name]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def save_pay(self, discordID, invoice_id):
try:
with self.connection.cursor() as cursor:
cursor.execute("""update `store` set invoice_id = %s where id= %s""", (invoice_id, discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_pay(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select invoice_id from `store` where id=%s""", (discordID,))
self.connection.commit()
return [True, cursor.fetchone()]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def delete_pay(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute(
"""update `store` set invoice_id = NULL where id=%s""", (discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_money(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select money from `store` where id = %s""", (discordID,))
self.connection.commit()
return [True, cursor.fetchone()]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def add_money(self, discordID, money):
try:
with self.connection.cursor() as cursor:
cursor.execute("""update `store` set money = `money` + %s where id= %s""", (money, discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def add_money_username(self, username, money):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select id from `users` where username=%s""", (username,))
discordID = cursor.fetchone()['id']
cursor.execute("""update `store` set money = `money` + %s where id=%s""", (money, discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def remove_money(self, discordID, money):
try:
with self.connection.cursor() as cursor:
cursor.execute("""update `store` set money = `money` - %s where id=%s""", (money, discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_date(self, data):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select id from `store` where data_trial=%s""", (data,))
self.connection.commit()
return cursor.fetchall()
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [None]
def check_date_3day(self):
try:
data = datetime.date.today().strftime('%m%d')
day3 = datetime.date.today() + datetime.timedelta(days=3)
with self.connection.cursor() as cursor:
cursor.execute("""SELECT id FROM `store` WHERE DATE_FORMAT(data_trial,'%%m%%d') >= %s AND DATE_FORMAT(data_trial,'%%m%%d') <= %s""", (data, day3.strftime('%m%d')))
self.connection.commit()
return cursor.fetchall()
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [None]
def add_data(self, data, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""update `store` set data_trial=%s where id=%s""", (data, discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def remove_data(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""update `store` set data_trial = NULL where id=%s""", (discordID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def unbane(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select hwidId from `users` where id=%s""", (discordID,))
ID = cursor.fetchone()['hwidId']
cursor.execute("""update `hwids` set banned = 0 where id=%s""", (ID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def bane(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select hwidId from `users` where id=%s""", (discordID,))
ID = cursor.fetchone()['hwidId']
cursor.execute("""update `hwids` set banned = 1 where id=%s""", (ID,))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_hwidId(self, discordID):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select hwidId from `users` where id=%s""", (discordID,))
self.connection.commit()
return cursor.fetchone()
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_discordID_toInvoice_id(self, invoice_id):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select id from `store` where invoice_id=%s""", (invoice_id,))
self.connection.commit()
return [True, cursor.fetchone()]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_promo(self, code):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select id, enabled, value from `promo` where code = %s""", (code,))
self.connection.commit()
res = cursor.fetchone()
return [True, res if res is not None else {'enabled': 0}]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def add_use_promo(self, code):
try:
with self.connection.cursor() as cursor:
cursor.execute("""update `promo` set `use`= `use` + 1 where code= %s""", (code, ))
self.connection.commit()
return [True]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [False, getError(ex)]
def check_uuid(self, nickname):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select uuid from `users` where username=%s""", (nickname,))
self.connection.commit()
return [True, cursor.fetchone()]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [None]
def check_banlist(self, page):
try:
with self.connection.cursor() as cursor:
limit = 10 * page
endLimit = limit - 10
if page <= 0:
limit = 0
endLimit = 0
cursor.execute("""select name, reason, operator, end from `punishments`
where punishmentType='BAN' OR punishmentType='TEMP_BAN' ORDER BY name LIMIT %s OFFSET %s""", (limit, endLimit,))
self.connection.commit()
return cursor.fetchall()
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [None]
def check_game_role(self, uuid):
try:
with self.connection.cursor() as cursor:
cursor.execute("""select permission from `luckperms_user_permissions` where uuid=%s""", (uuid,))
self.connection.commit()
return [True, cursor.fetchone()]
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [None]
def check_birthday(self, data):
try:
with self.connection.cursor() as cursor:
cursor.execute("""SELECT id FROM `users` WHERE DATE_FORMAT(birthday, '%%m%%d') = %s""", (data,))
self.connection.commit()
return cursor.fetchall()
except Exception as ex:
print(f'[SQL connector]\x1B[31m {ex}\033[0m')
return [None]