generated from CodeChefVIT/template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
562 lines (482 loc) · 26.4 KB
/
bot.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import discord
from discord.ext import commands
import os
import smtplib
import psycopg2
from datetime import date
from mask import encrypt_string,decrypt_string
from decouple import config
EMAIL_ADDRESS = config('userid')
EMAIL_PASSWORD = config('userpass')
def remove_quote(s):
s=str(s).split("'")
s="".join(s)
return s
def remove_quote_id(s,id):
s=str(s).split("'")
s="".join(s)
s=s+str(id)
return s
try:
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur = conn.cursor()
cur.execute('''CREATE TABLE DISCORDBOT
(SERVER VARCHAR(100) NOT NULL,
CHANNEL VARCHAR(100) NOT NULL,
USERNAME VARCHAR(50) NOT NULL,
USERID VARCHAR(50) NOT NULL,
MSGCNT INT,
EMAIL TEXT,
DATE TEXT,
ROLES VARCHAR(200));''')
conn.commit()
cur.execute('''CREATE TABLE MESSAGES
(SERVER VARCHAR(100) NOT NULL,
CHANNEL VARCHAR(100) NOT NULL,
MSGID VARCHAR(50) NOT NULL,
DATE TEXT,
ROLES VARCHAR(200));''')
conn.commit()
cur.close()
conn.close()
except:
pass
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
server_insert=remove_quote_id(str(message.guild),str(message.guild.id))
channel_insert=remove_quote_id(str(message.channel.name),str(message.channel.id))
username_insert=remove_quote(str(message.author.name))
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
role_str=[]
role=[]
try:
role=message.author.roles
except:
pass
for i in role:
role_str.append(str(i.name))
if(len(role_str)):
cur.execute("INSERT INTO MESSAGES (server,channel,MSGID,DATE,ROLES) VALUES (%s,%s,%s,%s,%s)", (str(server_insert),str(channel_insert), str(encrypt_string(str(message.id))), str(date.today()), str("!.#$%".join(role_str))))
print("(server,channel,MSGID,DATE,ROLES) inserted in messages table")
conn.commit()
else:
cur.execute("INSERT INTO MESSAGES (server,channel,MSGID,DATE) VALUES (%s,%s,%s,%s)", (str(server_insert),str(channel_insert), str(encrypt_string(str(message.id))), str(date.today()) ))
print("(server,channel,MSGID,DATE) inserted in messages table")
conn.commit()
# Counts the number of messages by each member
if message.author.bot:
pass
else:
role_str=[]
role=message.author.roles
for i in role:
role_str.append(str(i.name))
cur.execute("SELECT channel, username, msgcnt, email, userid from DISCORDBOT where server='%s' " % (str(server_insert)))
rows = cur.fetchall()
if(len(rows)):
flag=0
for row in rows:
if(row[0]==str(channel_insert) and row[1]==str(username_insert) and row[4]==str(message.author.id)):
flag=1
cur.execute("UPDATE DISCORDBOT set MSGCNT = %s, ROLES = %s where CHANNEL = %s and USERNAME = %s and SERVER = %s and USERID = %s", (int(int(row[2])+1), str("!.#$%".join(role_str)), str(channel_insert), str(username_insert),str(server_insert), str(message.author.id)))
conn.commit()
print("Updated Discord bot table")
break
if(flag==0):
cur.execute("INSERT INTO DISCORDBOT (server,channel,USERNAME,USERID,MSGCNT,EMAIL,DATE,ROLES) VALUES (%s,%s,%s,%s,1,'Not Updated',%s,%s)", (str(server_insert),str(channel_insert), str(username_insert), str(message.author.id), str(date.today()), str("!.#$%".join(role_str))))
print("(server,channel,USERNAME,MSGCNT,EMAIL,DATE,ROLES) inserted in DISCORDBOT table")
else:
cur.execute("INSERT INTO DISCORDBOT (server,channel,USERNAME,USERID,MSGCNT,EMAIL,DATE,ROLES) VALUES (%s,%s,%s,%s,1,'Not Updated',%s,%s)", (str(server_insert),str(channel_insert), str(username_insert), str(message.author.id), str(date.today()), str("!.#$%".join(role_str))))
print("(server,channel,USERNAME,MSGCNT,EMAIL,DATE,ROLES) inserted in DISCORDBOT table")
conn.commit()
print("Saved to DB")
if message.content == "!users": # To find number of users in the channel
await message.channel.send(f"# of Members: {message.guild.member_count}")
elif message.content == "!msgcnt": # To find number of messages sent by each users
cur.execute("SELECT username, msgcnt, date from DISCORDBOT where channel = '%s' and server = '%s' " % (str(channel_insert),str(server_insert)))
rows = cur.fetchall()
msg=""
for i in rows:
msg+=f"{i[0]}: {i[1]}, Last msg posted on {i[2]}"
msg+="\n"
await message.channel.send(msg)
elif str(message.content)[:7] == "!msgcnt":
username = message.mentions
msg=""
for j in range(len(username)):
cur.execute("SELECT username, msgcnt, date from DISCORDBOT where channel = '%s' and server = '%s' and username = '%s' and userid = '%s'" % (str(channel_insert), str(server_insert), str(remove_quote(str(username[j].name))), str(username[j].id)))
rows = cur.fetchall()
for i in rows:
msg+=f"{i[0]}: {i[1]}, Last msg posted on {i[2]}"
msg+="\n"
if(len(rows)==0):
msg+=f"{username[j].name}: 0, No messages made"
await message.channel.send(msg)
elif message.content == "!rstcnt": # To reset the count of messages of each user in a channel
cur.execute("DELETE from DISCORDBOT where CHANNEL = '%s' and server = '%s' " % (str(channel_insert),str(server_insert)))
await message.channel.send(f"Message count for this whole channel has been reset ")
print("A whole channel deleted in DISCORDBOT table")
conn.commit()
elif str(message.content)[:7] == "!rstcnt": # To reset the count of messages of for the specified role in a channel
role=message.role_mentions
cur.execute("SELECT username, msgcnt, date, userid, roles from DISCORDBOT where channel = '%s' and server = '%s' " % (str(channel_insert),str(server_insert)))
rows = cur.fetchall()
for i in rows:
for j in role:
list_split=str(i[-1]).split("!.#$%")
if(str(j.name) in list_split):
cur.execute("DELETE from DISCORDBOT where channel = '%s' and server = '%s' and username='%s' and date='%s' and roles='%s'" % (str(channel_insert),str(server_insert),str(i[0]),str(i[2]),str(i[-1])))
print("Rows containing a specific role deleted in DISCORDBOT table")
conn.commit()
if(len(role)):
await message.channel.send(f"Message count for the roles mentioned in this channel has been reset ")
username=message.mentions
for j in range(len(username)):
cur.execute("DELETE from DISCORDBOT where CHANNEL = '%s' and server = '%s' and username = '%s' and userid = '%s'" % (str(channel_insert),str(server_insert), str(remove_quote(str(username[j].name))), str(username[j].id)))
print("Row containing a specific user deleted in DISCORDBOT table")
conn.commit()
if(len(username)):
await message.channel.send(f"Message count for the usernames mentioned in this channel has been reset ")
elif str(message.content) == "!del week": # To delete messages in the starting week
cur.execute("SELECT channel, msgid, date from MESSAGES where channel='%s' and server = '%s' " % (str(channel_insert),str(server_insert)))
rows = cur.fetchall()
a=9999
b=9999
c=9999
for i in rows:
d=i[2]
a1,b1,c1=str(d).split("-")
a1=int(a1)
b1=int(b1)
c1=int(c1)
if(a>a1):
a=a1
b=b1
c=c1
elif(a==a1):
if(b>b1):
b=b1
c=c1
elif(b==b1):
if(c>c1):
c=c1
for row in rows:
d=row[2]
a1,b1,c1=str(d).split("-")
a1=int(a1)
b1=int(b1)
c1=int(c1)
if(0<=c and c<=7):
if(a1==a and b1==b and 0<=c1 and c1<=7):
messages=await message.channel.fetch_message(int(decrypt_string(str(row[1]))))
await messages.delete(delay=None)
cur.execute("DELETE from MESSAGES where MSGID='%s' and server = '%s' and channel = '%s' ;" % (row[1],str(server_insert),str(channel_insert)))
print("Rows with messages for a week deleted in MESSAGES table")
conn.commit()
elif(8<=c and c<=14):
if(a1==a and b1==b and 8<=c1 and c1<=14):
messages=await message.channel.fetch_message(int(decrypt_string(str(row[1]))))
await messages.delete(delay=None)
cur.execute("DELETE from MESSAGES where MSGID='%s' and server = '%s' and channel = '%s' ;" % (row[1],str(server_insert),str(channel_insert)))
print("Rows with messages for a week deleted in MESSAGES table")
conn.commit()
elif(15<=c and c<=21):
if(a1==a and b1==b and 15<=c1 and c1<=21):
messages=await message.channel.fetch_message(int(decrypt_string(str(row[1]))))
await messages.delete(delay=None)
cur.execute("DELETE from MESSAGES where MSGID='%s' and server = '%s' and channel = '%s' ;" % (row[1],str(server_insert),str(channel_insert)))
print("Rows with messages for a week deleted in MESSAGES table")
conn.commit()
elif(22<=c and c<=31):
if(a1==a and b1==b and 22<=c1 and c1<=31):
messages=await message.channel.fetch_message(int(decrypt_string(str(row[1]))))
await messages.delete(delay=None)
cur.execute("DELETE from MESSAGES where MSGID='%s' and server = '%s' and channel = '%s' ;" % (row[1],str(server_insert),str(channel_insert)))
print("Rows with messages for a week deleted in MESSAGES table")
conn.commit()
await message.channel.send(f"Messages for the first week in this channel has been deleted")
elif str(message.content)[:10] == "!del month": # To delete messages in the starting month
month_no=int("0"+(str(message.content)[10:]).strip())
cur.execute("SELECT channel, msgid, date from MESSAGES where channel='%s' and server = '%s' " % (str(channel_insert),str(server_insert)))
rows = cur.fetchall()
a=9999
b=9999
c=9999
for i in rows:
d=i[2]
a1,b1,c1=str(d).split("-")
a1=int(a1)
b1=int(b1)
c1=int(c1)
if(a>a1):
a=a1
b=b1
c=c1
elif(a==a1):
if(b>b1):
b=b1
c=c1
elif(b==b1):
if(c>c1):
c=c1
b=b+month_no
a+=b//12
b=(b-1)%12+1
for row in rows:
d=row[2]
a1,b1,c1=str(d).split("-")
a1=int(a1)
b1=int(b1)
c1=int(c1)
if(a1==a and b1==b):
messages=await message.channel.fetch_message(int(decrypt_string(str(row[1]))))
await messages.delete(delay=None)
cur.execute("DELETE from MESSAGES where MSGID='%s' and server = '%s' and channel = '%s' ;" % (row[1],str(server_insert),str(channel_insert)))
print("Rows with messages for a month deleted in MESSAGES table")
conn.commit()
await message.channel.send(f"Messages for the specified month in this channel has been deleted")
elif str(message.content)[:4] == "!del": # Delete messages by the roles
role_del = message.role_mentions
print(role_del)
cur.execute("SELECT channel, msgid, date, roles from MESSAGES where channel='%s' and server='%s' " % (str(channel_insert),str(server_insert)))
rows = cur.fetchall()
for row in rows:
for j in role_del:
list_split=str(row[3]).split("!.#$%")
if str(j.name) in list_split:
messages=await message.channel.fetch_message(int(decrypt_string(str(row[1]))))
await messages.delete(delay=None)
cur.execute("DELETE from MESSAGES where MSGID='%s' and server = '%s' and channel = '%s' ;" % (row[1],str(server_insert),str(channel_insert)))
print("Rows with messages from a role deleted in MESSAGES table")
conn.commit()
if(len(role_del)):
await message.channel.send(f"Message made by users having the mentioned roles in this channel has been deleted ")
elif str(message.content[:6]) == "!email": # Add the emails of the members
email_add=message.content[6:]
email_add=email_add.strip()
cur.execute("UPDATE DISCORDBOT set EMAIL = '%s' where channel = '%s' and username = '%s' and server = '%s' and userid = '%s'" % (str(email_add), str(channel_insert), str(username_insert),str(server_insert), str(message.author.id)))
print("Email of a person updated in DISCORDBOT table")
await message.channel.send(f"Email for the user {message.author.name} has been added in this channel")
conn.commit()
elif message.content == "!help": # To show all the possible options available with the bot
embed = discord.Embed(title="Help on BOT", description="Some useful commands")
embed.add_field(name="!users",value="Returns the number of users in the channel")
embed.add_field(name="!email <email id>",value="Sends an email when a pull request is made")
embed.add_field(name="!del <tag the roles>",value="Deletes the messages by members of the tagged roles")
embed.add_field(name="!del week",value="Deletes the messages in the starting week")
embed.add_field(name="!del month <Month number>",value="Deletes the messages in the starting month + the month number")
embed.add_field(name="!msgcnt",value="Returns the number of messages sent by each user")
embed.add_field(name="!msgcnt <tag the users>",value="Returns the number of messages sent by the tagged users")
embed.add_field(name="!rstcnt",value="Resets the number of messages of each user to Zero")
embed.add_field(name="!rstcnt <tag the roles>",value="Resets the number of messages of each user of the tagged roles to Zero")
embed.add_field(name="!rstcnt <name of the user>",value="Resets the number of messages of the tagged users to Zero")
embed.add_field(name="!online",value="Returns number of online members present")
embed.add_field(name="!role",value="Returns number of members under each role")
await message.channel.send(content=None, embed=embed)
elif message.content == "!online": # To find out number of online members
list_members = message.guild.members
count_online_members = message.guild.member_count
for i in list_members:
if str(i.status)=="offline" or bool(i.bot):
count_online_members-=1
await message.channel.send(f"# of Online Members: {count_online_members}")
elif message.content == "!role": # To find out number of members under each role
list_members = message.guild.members
roles_count = {}
msg=""
for i in list_members:
member_roles = i.roles
for j in member_roles:
if j in roles_count:
roles_count[j]+=1
else:
roles_count[j]=1
msg+=f"Roles :- Members"
msg+="\n"
for i in roles_count.keys():
if str(i.name)[0]=="@":
msg+=f"{str(i.name)[1:]} :- {roles_count[i]}"
msg+="\n"
else:
msg+=f"{str(i.name)} :- {roles_count[i]}"
msg+="\n"
await message.channel.send(msg)
if ("GitHub" in str(message.author.name) and message.author.bot): #To send emails when a Github Pull request is made
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
subject = "An update was made to the git repository"
body = "An update was made to the git repository\n\n"
for i in message.embeds:
body+=i.title+"\n\n"
body+=i.description+"\n\n\n\n"
msg = f"Subject: {subject}\n\n{body}"
cur.execute("SELECT email from DISCORDBOT where channel='%s' and server='%s'" % (str(channel_insert),str(server_insert)))
rows = cur.fetchall()
for i in rows:
if(i[0]!="Not Updated"):
s.sendmail(EMAIL_ADDRESS,i[0],msg)
print("Email sent")
s.quit()
conn.commit()
cur.close()
conn.close()
# To remove the member from the database
@client.event
async def on_member_remove(member):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
print("Member removed part")
print(str(member.name),str(member.guild))
cur.execute("DELETE from DISCORDBOT where USERNAME='%s' and server='%s' and userid = '%s';" % (str(remove_quote(str(member.name))),str(remove_quote_id(str(member.guild),str(member.guild.id))),str(member.id)))
conn.commit()
cur.close()
conn.close()
# To remove this channel from the database
@client.event
async def on_private_channel_delete(channel):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("DELETE from DISCORDBOT where channel='%s' and server='%s';" % (str(remove_quote_id(str(channel.name),str(channel.id))),str(remove_quote_id(str(channel.guild),str(channel.guild.id)))))
conn.commit()
cur.execute("DELETE from MESSAGES where channel='%s' and server='%s';" % (str(remove_quote_id(str(channel.name),str(channel.id))),str(remove_quote_id(str(channel.guild),str(channel.guild.id)))))
conn.commit()
cur.close()
conn.close()
# To remove this channel from the database
@client.event
async def on_guild_channel_delete(channel):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("DELETE from DISCORDBOT where channel='%s' and server='%s';" % (str(remove_quote_id(str(channel.name),str(channel.id))),str(remove_quote_id(str(channel.guild),str(channel.guild.id)))))
conn.commit()
cur.execute("DELETE from MESSAGES where channel='%s' and server='%s';" % (str(remove_quote_id(str(channel.name),str(channel.id))),str(remove_quote_id(str(channel.guild),str(channel.guild.id)))))
conn.commit()
cur.close()
conn.close()
# To remove the server from the database
@client.event
async def on_guild_remove(guild):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
print("Removed from guild")
cur.execute("DELETE from DISCORDBOT where server='%s';" % (str(remove_quote_id(str(guild),str(guild.id)))))
conn.commit()
cur.execute("DELETE from MESSAGES where server='%s';" % (str(remove_quote_id(str(guild),str(guild.id)))))
conn.commit()
cur.close()
conn.close()
# To update the channel in the database
@client.event
async def on_guild_channel_update(before, after):
print(str(after.name)+str(after.id), str(before.guild)+str(before.id))
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("UPDATE DISCORDBOT set CHANNEL = '%s' where server = '%s' " % (str(remove_quote_id(str(after.name),str(after.id))), str(remove_quote_id(str(before.guild),str(before.guild.id)))))
print("channel is updated DISCORDBOT table")
conn.commit()
cur.execute("UPDATE MESSAGES set CHANNEL = '%s' where server = '%s' " % (str(remove_quote_id(str(after.name),str(after.id))), str(remove_quote_id(str(before.guild),str(before.guild.id)))))
print("channel is updated MESSAGES table")
conn.commit()
cur.close()
conn.close()
# To update the channel in the database
@client.event
async def on_private_channel_update(before, after):
print(str(after.name)+str(after.id), str(before.guild)+str(before.id))
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("UPDATE DISCORDBOT set CHANNEL = '%s' where server = '%s' " % (str(remove_quote_id(str(after.name),str(after.id))), str(remove_quote_id(str(before.guild),str(before.guild.id)))))
print("channel is updated DISCORDBOT table")
conn.commit()
cur.execute("UPDATE MESSAGES set CHANNEL = '%s' where server = '%s' " % (str(remove_quote_id(str(after.name),str(after.id))), str(remove_quote_id(str(before.guild),str(before.guild.id)))))
print("channel is updated MESSAGES table")
conn.commit()
cur.close()
conn.close()
# To update the member in the database
@client.event
async def on_member_update(before, after):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
role_str=[]
role=after.roles
for i in role:
role_str.append(str(i.name))
cur.execute("UPDATE DISCORDBOT set ROLES = '%s' where server = '%s' and username = '%s' and userid = '%s'" % (str("!.#$%".join(role_str)), str(remove_quote_id(str(before.guild),str(before.guild.id))), str(remove_quote(str(after.name))), str(after.id)))
print("roles is updated DISCORDBOT table")
conn.commit()
cur.close()
conn.close()
# To update the member in the database
@client.event
async def on_user_update(before, after):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
print(str(after.name), str(before.name), str(before.id))
cur = conn.cursor()
cur.execute("UPDATE DISCORDBOT set USERNAME = '%s' where username = '%s' and userid = '%s'" % (str(remove_quote(str(after.name))), str(remove_quote(str(before.name))), str(before.id)))
print("roles is updated DISCORDBOT table")
conn.commit()
cur.close()
conn.close()
# To update the channel in the database
@client.event
async def on_guild_update(before, after):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("UPDATE DISCORDBOT set CHANNEL = '%s' where server = '%s' " % (str(remove_quote_id(str(after.name),str(after.id)), str(remove_quote_id(str(before.guild),str(before.guild.id))))))
print("channel is updated DISCORDBOT table")
conn.commit()
cur.execute("UPDATE MESSAGES set CHANNEL = '%s' where server = '%s' " % (str(remove_quote_id(str(after.name),str(after.id)), str(remove_quote_id(str(before.guild),str(before.guild.id))))))
print("channel is updated MESSAGES table")
conn.commit()
cur.close()
conn.close()
# To update the role
@client.event
async def on_guild_role_update(before, after):
conn = psycopg2.connect(database = config('database'), user = config('user'), password = config('password'), host = config('host'), port = config('port'))
print ("Opened database successfully")
cur = conn.cursor()
cur.execute("SELECT channel, username, userid, roles from DISCORDBOT where server='%s' " % (str(remove_quote_id(str(before.guild),str(before.guild.id)))))
rows = cur.fetchall()
for row in rows:
list_split=str(row[3]).split("!.#$%")
if str(before.name) in list_split:
index_list=list_split.index(before.name)
list_split[index_list]=str(after.name)
cur.execute("UPDATE DISCORDBOT set roles = '%s' where username = '%s' and userid = '%s' and server = '%s' and channel = '%s' ;" % ("!.#$%".join(list_split), str(row[1]), str(row[2]), str(remove_quote_id(str(before.guild),str(before.guild.id))), str(row[0])))
print("Rows with updated roles updated in MESSAGES table")
conn.commit()
conn.commit()
cur.execute("SELECT channel, msgid, roles from MESSAGES where server='%s' " % (str(remove_quote_id(str(before.guild),str(before.guild.id)))))
rows = cur.fetchall()
for row in rows:
list_split=str(row[2]).split("!.#$%")
if str(before.name) in list_split:
index_list=list_split.index(before.name)
list_split[index_list]=str(after.name)
cur.execute("UPDATE MESSAGES set roles = '%s' where MSGID='%s' and server = '%s' and channel = '%s' ;" % ("!.#$%".join(list_split), str(row[1]), str(remove_quote_id(str(before.guild),str(before.guild.id))), str(row[0])))
print("Rows with updated roles updated in MESSAGES table")
conn.commit()
conn.commit()
cur.close()
conn.close()
client.run(config('token'))