This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
storage.py
462 lines (388 loc) · 16.1 KB
/
storage.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import discord
from datetime import datetime
import mysql.connector, json, sys
class Storage:
def __init__(self):
self.DBUsername = None
self.DBPassword = None
self.DBName = None
try:
with open('config.json') as f:
c = json.load(f)
self.DBUsername = c.get('db_username')
self.DBPassword = c.get('db_password')
self.DBName = c.get('db_name')
if not all((
self.DBUsername,
self.DBPassword,
self.DBName,
)):
raise KeyError('Missing field.')
except FileNotFoundError:
self.save()
print('Please edit config.json')
sys.exit(1)
except KeyError as f:
self.save()
print(f'Missing value: {f}')
sys.exit(1)
self.conn = mysql.connector.connect(
host='localhost',
user=self.DBUsername,
password=self.DBPassword,
database=self.DBName
)
self.datetime_scheme = '%Y-%m-%d %H:%M:%S'
def save(self):
with open('config.json', 'w') as f:
json.dump({
'db_username': self.DBUsername,
'db_password': self.DBPassword,
'db_name': self.DBName,
}, f)
def get_cursor(self, retry: bool = True):
try:
cursor = self.conn.cursor(buffered=True)
return cursor
except Exception as e:
if retry:
self.conn = mysql.connector.connect(
host='localhost',
user=self.DBUsername,
passwd=self.DBPassword,
database=self.DBName
)
return self.get_cursor(retry=False)
else:
raise e
def execute_query(self, query: str, commit: bool = True):
cur = self.get_cursor()
cur.execute(query)
if commit:
self.conn.commit()
r = cur.fetchone()
cur.close()
return r
def execute_query_many(self, query: str, commit: bool = True):
cur = self.get_cursor()
cur.execute(query)
if commit:
self.conn.commit()
r = cur.fetchall()
cur.close()
return r
'''
CLEAN UP AND GENERAL FUNCTIONS
'''
def add_guild(self, guild: discord.Guild, role: discord.Role = None):
if role:
query = f'INSERT INTO settings (gid, lfc_role) VALUES ({guild.id}, {role.id});'
else:
query = f'INSERT INTO settings (gid) VALUES ({guild.id});'
self.execute_query(query, commit=True)
def get_all_guilds(self, client: discord.Client):
query = f'SELECT gid FROM settings;'
r = self.execute_query_many(query)
return [client.get_guild(gid[0]) for gid in r]
def guild_leave(self, guild: discord.Guild):
query = f'DELETE FROM messages WHERE gid={guild.id};'
self.execute_query(query)
query = f'DELETE from settings WHERE gid={guild.id};'
self.execute_query(query)
query = f'DELETE from lfc_channels WHERE gid={guild.id};'
self.execute_query(query)
query = f'DELETE from profile_channels WHERE gid={guild.id};'
self.execute_query(query)
query = f'DELETE from auto_voice_names WHERE gid={guild.id};'
self.execute_query(query, commit=True)
def user_join(self, user: discord.Member):
query = f'INSERT INTO sot_profile (uid) \
SELECT {user.id} \
FROM dual \
WHERE NOT EXISTS (\
SELECT uid \
FROM sot_profile \
WHERE uid={user.id}\
) LIMIT 1;'
self.execute_query(query)
query = f'INSERT INTO gamertags (uid) \
SELECT {user.id} \
FROM dual \
WHERE NOT EXISTS (\
SELECT uid \
FROM gamertags \
WHERE uid={user.id}\
) LIMIT 1;'
self.execute_query(query)
query = f'INSERT INTO social (uid) \
SELECT {user.id} \
FROM dual \
WHERE NOT EXISTS (\
SELECT uid \
FROM social \
WHERE uid={user.id}\
) LIMIT 1;'
self.execute_query(query)
def user_leave(self, user: discord.User):
query = f'DELETE FROM sot_profile WHERE uid={user.id};'
self.execute_query(query, commit=False)
query = f'DELETE FROM gamertags WHERE uid={user.id};'
self.execute_query(query, commit=False)
query = f'DELETE FROM social WHERE uid={user.id};'
self.execute_query(query)
'''
`PROFILE` FUNCTIONS
'''
async def get_sot_profile(self, ctx, user: discord.Member):
query = f'SELECT hc,sd,gh,oos,ma,af,img,alias,rb FROM sot_profile WHERE uid={user.id};'
cur = self.get_cursor()
cur.execute(query)
r = cur.fetchone()
#If Profile doesn't exist yet
if cur.rowcount == 0 or not r:
await self.create_profile(ctx, user)
return None
cur.close()
profile = {
'hc': r[0],
'sd': r[1],
'gh': r[2],
'oos': r[3],
'ma': r[4],
'rb': r[8],
'af': r[5],
'img': r[6],
'alias': r[7]
}
profile['gtag'] = self.get_xbox_tag(user)
return profile
async def get_tag_profile(self, ctx, user: discord.Member):
query = f'SELECT steam,xbox,psn,nintendo,minecraft,origin,blizzard,bethesda,gog FROM gamertags WHERE uid={user.id};'
r = self.execute_query(query)
if not r or len(r) == 0:
await self.create_profile(ctx, user)
return None
profile = {
'steam': r[0],
'xbox': r[1],
'psn': r[2],
'nintendo': r[3],
'minecraft': r[4],
'origin': r[5],
'blizzard': r[6],
'bethesda': r[7],
'gog': r[8]
}
return profile
async def get_social_profile(self, ctx, user: discord.Member):
query = f'SELECT twitch,mixer,youtube,twitter,reddit,itchio FROM social WHERE uid={user.id}'
r = self.execute_query(query)
if not r or len(r) == 0:
await self.create_profile(ctx, user)
return None
profile = {
'twitch': r[0],
'mixer': r[1],
'youtube': r[2],
'twitter': r[3],
'reddit': r[4],
'itchio': r[5]
}
return profile
def get_xbox_tag(self, user: discord.Member):
query = f'SELECT xbox FROM gamertags WHERE uid={user.id};'
r = self.execute_query(query)
if not r:
return 'None'
return r[0] or 'None'
async def create_profile(self, ctx, user: discord.Member):
self.user_join(user)
embed = discord.Embed(
title='Profile Created',
colour=0xffd700,
description='For a full documentation of what you can change please type `?help Profile`.',
timestamp=datetime.utcnow()
)
embed.set_author(name=user, icon_url=user.avatar_url)
await ctx.send(embed=embed)
def update_levels(self, user: discord.Member, comps: dict()):
cur = self.get_cursor()
for comp, lvl in comps.items():
cur.execute(f'UPDATE sot_profile SET {comp}={lvl} WHERE uid={user.id};')
self.conn.commit()
cur.close()
def update_gamertag(self, user: discord.Member, platform: str, gamertag: str):
if gamertag.lower() == 'null':
query = f'UPDATE gamertags SET {platform}=NULL WHERE uid={user.id};'
else:
query = f'UPDATE gamertags SET {platform}="{gamertag}" WHERE uid={user.id};'
self.execute_query(query, commit=True)
def update_img(self, user: discord.Member, url: str):
if not url:
query = f'UPDATE sot_profile SET img=NULL WHERE uid={user.id};'
else:
query = f'UPDATE sot_profile SET img="{url}" WHERE uid={user.id};'
self.execute_query(query, commit=True)
def update_alias(self, user: discord.Member, alias: str):
if not alias:
query = f'UPDATE sot_profile SET alias=NULL WHERE uid={user.id};'
else:
query = f'UPDATE sot_profile SET alias="{alias}" WHERE uid={user.id};'
self.execute_query(query, commit=True)
def update_social_media(self, user: discord.Member, platform: str, username: str):
if username.lower() == 'null':
query = f'UPDATE social SET {platform}=NULL WHERE uid={user.id};'
else:
query = f'UPDATE social SET {platform}="{username}" WHERE uid={user.id};'
self.execute_query(query, commit=True)
'''
`LOOKING FOR CREW` FUNCTIONS
'''
'''
`LOOKING FOR CREW`-MODULE SETTINGS
'''
def get_lfc_settings(self, guild: discord.Guild):
settings = dict()
query = f'SELECT lfc FROM settings WHERE gid={guild.id};'
settings['status'] = self.execute_query(query)[0] == True
query = f'SELECT cid FROM lfc_channels WHERE gid={guild.id};'
r = self.execute_query_many(query)
settings['channels'] = [guild.get_channel(c[0]) for c in r]
settings['role'] = self.get_lfc_role(guild)
return settings
def update_lfc_status(self, guild: discord.Guild, status: bool):
query = f'UPDATE settings SET lfc={str(status)} WHERE gid={guild.id};'
self.execute_query(query, commit=True)
def add_lfc_channels(self, guild: discord.Guild, channels: list()):
self.delete_all_lfc_channels(guild)
cur = self.get_cursor()
cids = list()
for c in channels:
cids.append((str(c.id), str(guild.id)))
cur.executemany(f'INSERT INTO lfc_channels (cid,gid) VALUES (%s,%s);', cids)
self.conn.commit()
cur.close()
def delete_all_lfc_channels(self, guild: discord.Guild):
query = f'DELETE FROM lfc_channels WHERE gid={guild.id};'
self.execute_query(query, commit=True)
def get_lfc_enabled_guilds(self, client: discord.Client):
query = 'SELECT gid FROM settings WHERE lfc=TRUE;'
r = self.execute_query_many(query)
return [client.get_guild(gid[0]) for gid in r]
def update_lfc_role(self, guild: discord.Guild, role: discord.Role):
query = f'UPDATE settings SET lfc_role={role.id} WHERE gid={guild.id}'
self.execute_query(query, commit=True)
def get_lfc_role(self, guild: discord.Guild):
query = f'SELECT lfc_role FROM settings WHERE gid={guild.id}'
r = self.execute_query(query)[0]
return guild.get_role(r)
'''
`PROFILE`-MODULE SETTINGS
'''
def get_profile_settings(self, guild: discord.Guild):
settings = dict()
query = f'SELECT profile FROM settings WHERE gid={guild.id};'
settings['status'] = self.execute_query(query)[0] == True
query = f'SELECT cid FROM profile_channels WHERE gid={guild.id};'
r = self.execute_query_many(query)
settings['channels'] = [guild.get_channel(c[0]) for c in r]
return settings
def update_profile_status(self, guild:discord.Guild, status: bool):
query = f'UPDATE settings SET profile={str(status)} WHERE gid={guild.id};'
self.execute_query(query, commit=True)
def add_profile_channels(self, guild: discord.Guild, channels: list()):
self.delete_all_profile_channels(guild)
cur = self.get_cursor()
cids = list()
for c in channels:
cids.append((str(c.id), str(guild.id)))
cur.executemany(f'INSERT INTO profile_channels (cid,gid) VALUES (%s,%s);', cids)
self.conn.commit()
cur.close()
def delete_all_profile_channels(self, guild: discord.Guild):
query = f'DELETE FROM profile_channels WHERE gid={guild.id};'
self.execute_query(query, commit=True)
def get_profile_enabled_guilds(self, client: discord.Client):
query = 'SELECT gid FROM settings WHERE profile=TRUE;'
r = self.execute_query_many(query)
return [client.get_guild(gid[0]) for gid in r]
'''
`AUTO-VOICE`-MODULE SETTINGS
'''
def get_auto_voice_settings(self, guild: discord.Guild):
settings = dict()
settings['channel'] = self.get_auto_voice_channel(guild)
settings['names'] = self.get_auto_voice_names(guild)
return settings
def get_auto_voice_channel(self, guild: discord.Guild):
query = f'SELECT auto_voice_channel FROM settings WHERE gid={guild.id};'
return guild.get_channel(self.execute_query(query)[0])
def get_auto_voice_names(self, guild:discord.Guild):
query = f'SELECT name FROM auto_voice_names WHERE gid={guild.id};'
r = self.execute_query_many(query)
return [n[0] for n in r]
def update_auto_voice_channel(self, guild:discord.Guild, channel: discord.abc.GuildChannel):
if channel:
query = f'UPDATE settings SET auto_voice_channel={channel.id} WHERE gid={guild.id};'
else:
query = f'UPDATE settings SET auto_voice_channel=NULL WHERE gid={guild.id};'
self.execute_query(query, commit=True)
def add_auto_voice_names(self, guild:discord.Guild, names: list()):
cur = self.get_cursor()
namelist = list()
for name in names:
namelist.append((name, guild.id))
cur.executemany(f'INSERT INTO auto_voice_names (name,gid) VALUES (%s, %s);', namelist)
self.conn.commit()
cur.close()
def delete_auto_voice_names(self, guild:discord.Guild, names: list()):
cur = self.get_cursor()
namelist = list()
for name in names:
namelist.append((name, str(guild.id)))
cur.executemany(f'DELETE FROM auto_voice_names WHERE name=%s and gid=%s;', namelist)
self.conn.commit()
cur.close()
def delete_all_auto_voice_names(self, guild:discord.Guild):
query = f'DELETE FROM auto_voice_names WHERE gid={guild.id};'
self.execute_query(query, commit=True)
'''
`ACTIVITY-LOGGING` SETTINGS
'''
def get_activity_logging_status(self, guild: discord.Guild):
query = f'SELECT activity_logging FROM settings WHERE gid={guild.id}'
r = self.execute_query(query)[0] == True
return r
def update_activity_logging_status(self, guild:discord.Guild, status: bool):
query = f'UPDATE settings SET activity_logging={str(status)} WHERE gid={guild.id}'
self.execute_query(query, commit=True)
'''
`ACTIVITY-LOGGING` FUNCTIONS
'''
def cleanup_messages(self, guilds:list()):
cur = self.get_cursor()
query = f'DELETE FROM messages WHERE timestamp < DATE_SUB(NOW(), INTERVAL 30 DAY) OR gid NOT IN ({",".join(str(g.id) for g in guilds)});'
cur.execute(query)
self.conn.commit()
count = cur.rowcount
cur.close()
return count
def user_leave_guild(self, user:discord.Member):
query = f'DELETE FROM messages WHERE uid={user.id} and gid={user.guild.id};'
self.execute_query(query, commit=True)
def add_message(self, m: discord.Message):
timestamp = m.created_at.strftime(self.datetime_scheme)
query = f'INSERT INTO messages (mid,uid,gid,timestamp) VALUES ({m.id}, {m.author.id}, {m.guild.id}, "{timestamp}");'
self.execute_query(query, commit=True)
def get_user_activity(self, user:discord.Member):
info = dict()
query = f'SELECT COUNT(*) FROM messages WHERE uid={user.id} AND gid={user.guild.id};'
info['amnt'] = self.execute_query(query)[0] or 0
query = f'SELECT timestamp FROM messages WHERE uid={user.id} AND gid={user.guild.id} ORDER BY timestamp DESC LIMIT 1;'
ts = self.execute_query(query)
info['timestamp'] = ts[0] if ts else None
return info
def get_activity_logging_enabled_guilds(self, client: discord.Client):
query = 'SELECT gid FROM settings WHERE activity_logging=TRUE;'
r = self.execute_query_many(query)
return [client.get_guild(gid[0]) for gid in r]