This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
584 lines (410 loc) · 19.3 KB
/
run.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import config
import discord
import pickle
from discord.ext import commands
from src.mongo_work import core as db
from traceback import format_exc
from src.bot_work import core
# Load pickles XXXXXXXX---XXXXXXXX
try:
with open(config.directory + "//pickle-db//embedAds.pickle", "rb") as handler:
config.ads_embed = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN EMBED ADS HAVE BEEN LOADED
print("!!!EMBED ADS HAVE BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN EMBED ADS ISN'T ABLE TO BE LOADED
print("!!!COULDN'T LOAD EMBED ADS!!!")
try:
with open(config.directory + "//pickle-db//textAds.pickle", "rb") as handler:
config.ads_text = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN TEXT ADS HAVE BEEN LOADED
print("!!!TEXT ADS HAVE BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN TEXT ADS ISN'T ABLE TO BE LOADED
print("!!!COULDN'T LOAD TEXT ADS!!!")
try:
with open(config.directory + "//pickle-db//whiteListServersAds.pickle", "rb") as handler:
config.ads_queue = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN WHITE LIST SERVERS ADS HAS BEEN LOADED
print("!!!WHITE LIST SERVERS HAVE BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN WHITE LIST SERVERS ADS ISN'T ABLE TO BE LOADED
print("!!!COULDN'T LOAD WHITE LIST SERVER ADS!!!")
try:
with open(config.directory + "//pickle-db//excludedListServersAds.pickle", "rb") as handler:
config.ads_excluded = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN EXCLUDED SERVERS HAS BEEN LOADED
print("!!!EXCLUDED SERVERS HAVE BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN EXCLUDED SERVERS ADS ISN'T ABLE TO LOADED
print("!!!COULDN'T LOAD EXCLUDED SERVER ADS!!!")
try:
with open(config.directory + "//pickle-db//lotterySettings.pickle", "rb") as handler:
config.lottery_settings = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN LOTTERY SETTINGS HAS BEEN LOADED
print("!!!LOTTERY SETTINGS HAS BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN LOTTERY SETTINGS ISN'T ABLE TO LOADED
print("!!!COULDN'T LOAD LOTTERY SETTINGS!!!")
try:
with open(config.directory + "//pickle-db//lotteryQueue.pickle", "rb") as handler:
config.lottery_queue = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN LOTTERY QUEUE HAS BEEN LOADED
print("!!!LOTTERY QUEUE HAS BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN LOTTERY QUEUE ISN'T ABLE TO BE LOADED
print("!!!COULDN'T LOAD LOTTERY QUEUE!!!")
try:
with open(config.directory + "//pickle-db//lotteryExcluded.pickle", "rb") as handler:
config.lottery_excludes = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN LOTTERY EXCLUDED HAS BEEN LOADED
print("!!!LOTTERY EXCLUDED HAS BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN LOTTERY EXCLUDED ISN'T ABLE TO BE LOADED
print("!!!COULDN'T LOAD LOTTERY EXCLUDED!!!")
try:
with open(config.directory + "//pickle-db//memberCount.pickle", "rb") as handler:
config.member_count = pickle.load(handler)
# FOR DEBUGGING
# PRINT WHEN MEMBER COUNT HAS BEEN LOADED
print("!!!MEMBER COUNT HAS BEEN LOADED!!!")
except:
# FOR DEBUGGING
# PRINT WHEN MEMBER COUNT ISN'T ABLE TO BE LOADED
print("!!!COULDN'T LOAD MEMBER COUNT!!!")
# START BOT CODE <-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
bot = commands.Bot(command_prefix=str(config.default_prefix), description=str(config.bot_description))
@bot.event
async def on_ready():
"""When the bot is ready to function and serve users."""
# FOR DEBUGGING
# PRINT BOT'S NAME AND BOT'S USER ID
print('\nLogged In As: ' + bot.user.name + " |ID: " + bot.user.id + "\n\n") # PRINT the IDENTIFIERS of the BOT
# Setup the bot's playing status.
await bot.change_presence(game=discord.Game(name=str(config.default_prefix) + "help"))
@bot.event
async def on_message(message):
"""Whenever a message is inputted via Discord chat."""
# If this is a bot don't process the commands, to prevent spam.
if message.author.bot == True:
return
try:
# Fetch the prefix via the server's ID.
prefix = db.discordServerDictionary(message.server.id)
# If no server data is found assume prefix to be default.
if prefix == False:
prefix = str(config.default_prefix)
else:
try:
prefix = prefix["general"]["prefix"]
except:
prefix = str(config.default_prefix)
bot.command_prefix = [prefix] # Use the fetched prefix as server prefix
except:
bot.command_prefix = [str(config.default_prefix)] # Use default prefix as server prefix
# Send the message to be processed by the bot
await bot.process_commands(message)
@bot.event
async def on_command_error(event, *args, **kwargs):
"""Whenever the bot experiences an error(eg. command not found)."""
raw = args[0]
# Check if this error came from a private channel(eg. direct message).
if raw.message.channel.is_private == True:
return
# Fetches the servers data with the server's ID.
data = db.discordServerDictionary(raw.message.server.id)
# If no server data is found then return.
if data in [False, None]:
return
# If server data is found, but no custom commands were found then return.
elif data["general"]["customCommands"] == {}:
return
# Get the first word after the prefix; this would in theory be a "custom command".
command_name = str(list(str(raw.message.content).split(" "))[0]).replace(data["general"]["prefix"], "")
# If command_name is one of the servers custom command do the following.
if command_name in data["general"]["customCommands"]:
# If custom command responds should be sent to the server
if data["general"]["customCommands"][command_name]["destination"] == "here":
if data["general"]["botChannel"] != False:
# Get a channel object from what's in bot channel
destination = bot.get_channel(data["general"]["botChannel"])
# Send to wherever called from if a bot cannot find channel
if destination in [False, None]:
destination = raw.message.channel
else:
# Send to wherever called from if a bot channel is not setup
destination = raw.message.channel
# If custom command responds should be sent to the authors pm
else:
destination = raw.message.author
msg_object = data["general"]["customCommands"][command_name]
# Add necessary stuff
msg_object["from"] = str(raw.message.server)
try:
msg_object["to"] = raw.message.author.mention
except:
msg_object["to"] = str(raw.message.author)
# Send response as embed
if msg_object["embed"] == True:
await bot.send_message(destination, embed=core.generateAnnouncement(msg_object))
# Send response as markdown
else:
await bot.send_message(destination, core.generateAnnouncement(msg_object))
@bot.event
async def on_server_remove(server):
"""Whenever a server removes the bot."""
try:
if db.removeDiscordServer(server.id) == False:
# FOR DEBUGGING
# PRINT WHEN A SERVER HAS REMOVED THE BOT FROM ITSELF, AND SUCCESSFULLY FROM DB DB
print("!!!" + str(server) + " SERVER DATA WASN'T REMOVED!!!")
else:
# FOR DEBUGGING
# PRINT WHEN A SERVER HAS REMOVED THE BOT FROM ITSELF, AND UNSUCCESSFULLY FROM OUR DB
print("!!!REMOVED " + str(server) + " SERVER DATA!!!")
# Either way send this message to the owner of the server.
await bot.send_message(server.owner, "I've been **successfully removed** from your server, **" + str(server) + "**, have a great day! :wave:")
except:
# FOR DEBUGGING
# PRINT WHEN REMOVING A SERVER CAUSED AN ERROR
print("!!!SOMETHING WENT WRONG WHILE REMOVING " + str(server) + " SERVER DATA!!!\nERROR:\n" + str(format_exc()))
@bot.event
async def on_server_join(server):
"""Whenever a server adds the bot."""
try:
if db.addDiscordServer(server.id) == False:
await bot.send_message(server.owner, "**Something went wrong** while **joining your server, " + str(server) + ",** you might want to **re-invite me!** :grimacing:")
return
await bot.send_message(server.owner, "I've successfully **joined your server, " + str(server) + ",** thank you for choosing *ComputerBot*! :smile:\nFor a list of commands enter **" + config.default_prefix + "help** or visit the bots **docs**, " + config.bot_docs + ".\nYou can also **configure me on your server** with the **server commands** take a look at them with **" + config.default_prefix + "help server**")
except:
# FOR DEBUGGING
# PRINT WHEN ADDING A SERVER CAUSED AN ERROR
print("!!!SOMETHING WENT WRONG WHILE TRYING TO ADD " + str(server) + " SERVER DATA!!!\nERROR:\n" + str(format_exc()))
@bot.event
async def on_member_join(member):
"""Whenever a user joins a server."""
data = db.discordServerDictionary(member.server.id)
# If no data, in our DB, is found on the server.
if data in [False, None]:
return
elif data["general"]["memberJoin"] == {}:
return
else:
# FOR DEBUGGING
# print("MEMBER JOIN EVENTS: " + str(data["general"]["memberJoin"]))
if "announce" in data["general"]["memberJoin"]:
# FOR DEBUGGING
# print("!!!ANNOUNCING THE NEW MEMBER!!!")
# Destination to send to
if data["general"]["memberJoin"]["announce"]["destination"] != "pm":
# Get a channel object from what's in destination
destination = bot.get_channel(data["general"]["memberJoin"]["announce"]["destination"])
# Send to servers general chat if a bot cannot find channel
if destination in [False, None]:
destination = member.server
# If should be sent to members pms
else:
destination = member
# FOR DEBUGGING
# print("DESTINATION: " + str(destination))
msg_object = data["general"]["memberJoin"]["announce"]
# FOR DEBUGGING
# print("MEMBER DIR: " + str(dir(member)))
msg_object["from"] = str(member.server)
msg_object["to"] = member.mention
# FOR DEBUGGING
# print("MSG OBJECT: " + str(msg_object))
if msg_object["embed"] == True:
await bot.send_message(destination, embed=core.generateAnnouncement(msg_object))
else:
await bot.send_message(destination, core.generateAnnouncement(msg_object))
if "giveRole" in data["general"]["memberJoin"]:
# FOR DEBUGGING
# print("!!!GIVING ROLE TO NEW MEMBER!!!")
giveRole = None
for role in member.server.roles:
if role.id == data["general"]["memberJoin"]["giveRole"]["role"]:
giveRole = role
# We got what we wanted so break
break
if giveRole == None:
# FOR DEBUGGING
# print("!!!COULD NOT GIVE ROLE BECAUSE ROLE DOESN'T EXIST!!!")
return
# FOR DEBUGGING
# print("GIVE ROLE: " + str(giveRole))
try:
await bot.add_roles(member, giveRole)
except Exception as e:
print("!!!CAN'T GIVE ROLE!!!\nERROR:\n" + str(e) + "\nFORMAT: " + str(format_exc()))
@bot.event
async def on_member_remove(member):
"""Whenever a user leaves a server."""
# FOR DEBUGGING
# print("Member Leave:\nMember: " + str(member))
# FOR DEBUGGING
# print("SERVER ID: " + str(member.server.id))
data = db.discordServerDictionary(member.server.id)
# FOR DEBUGGING
# print("DATA: " + str(data))
if data in [False, None]:
# FOR DEBUGGING
# print("!!!NO SERVER DATA!!!")
return
elif data["general"]["memberLeave"] == {}:
# FOR DEBUGGING
# print("!!!NO MEMBER LEAVE EVENTS!!!")
return
else:
# FOR DEBUGGING
# print("MEMBER LEAVE EVENTS: " + str(data["general"]["memberLeave"]))
if "announce" in data["general"]["memberLeave"]:
# FOR DEBUGGING
# print("!!!ANNOUNCING THE LEAVING MEMBER!!!")
# Destination to send to
if data["general"]["memberLeave"]["announce"]["destination"] != "pm":
# Get a channel object from what's in destination
destination = bot.get_channel(data["general"]["memberLeave"]["announce"]["destination"])
# Send to servers general chat if a bot cannot find channel
if destination in [False, None]:
destination = member.server
# If should be sent to members pms
else:
destination = member
# FOR DEBUGGING
# print("DESTINATION: " + str(destination))
msg_object = data["general"]["memberLeave"]["announce"]
msg_object["from"] = str(member.server)
try:
msg_object["to"] = member.mention
except:
msg_object["to"] = str(member)
# FOR DEBUGGING
# print("MSG OBJECT: " + str(msg_object))
if msg_object["embed"] == True:
await bot.send_message(destination, embed=core.generateAnnouncement(msg_object))
else:
await bot.send_message(destination, core.generateAnnouncement(msg_object))
@bot.event
async def on_channel_delete(channel):
"""Whenever a server deleted a channel."""
try:
server = channel.server
# Get the servers data
data = db.discordServerDictionary(server.id)
# FOR DEBUGGING
# print("SERVER DATA: " + str(data))
if data == False or data == None:
# FOR DEBUGGING
# print("!!!NO DATA ON " + str(server) + " A DISCORD SERVER!!!")
return
# FOR DEBUGGING
# print("CHANNEL DELETED: " + str(channel.id))
# print("BOT CHANNEL: " + str(data["general"]["botChannel"]))
if channel.id == data["general"]["botChannel"]:
# print("!!!SETTING BOT CHANNEL TO FALSE!!!")
if db.updateDiscordServer(server.id, {"general.botChannel": False}) == False:
# FOR DEBUGGING
# print("!!!CAN'T UPDATE BOT SERVER CHANNEL!!!")
return
# Msg server owner about the update to the bot's channel
await bot.send_message(server.owner, "Bot's **channel, " + str(channel) + ", in " + str(server) + " has been deleted** which means the **bot, " + config.bot_name + ", will now respond from wherever it's called on...** :eyes:")
else:
# FOR DEBUGGING
# print("!!!NOTHING TO CHANGE!!!")
pass
# Member join events
if data["general"]["memberJoin"] == {}:
# FOR DEBUGGING
# print("!!!NO MEMBER JOIN EVENTS!!!")
pass
else:
if "announce" in data["general"]["memberJoin"]:
# FOR DEBUGGING
# print("!!!THERE IS A ANNOUNCE JOINING MEMBER EVENT!!!")
# Check if the announce event is on
if data["general"]["memberLeave"]["announce"]["to"] == channel.id:
# FOR DEBUGGING
# print("MEMBER JOIN ANNOUNCEMENT CHANNEL IS REMOVED: " + str(channel))
pass
# Member leave events
if data["general"]["memberLeave"] == {}:
# FOR DEBUGGING
# print("!!!NO MEMBER LEAVE EVENTS!!!")
pass
else:
if "announce" in data["general"]["memberLeave"]:
# FOR DEBUGGING
# print("!!!THERE IS A ANNOUNCE LEAVING MEMBER EVENT!!!")
# Check if the announce event is on
if data["general"]["memberLeave"]["announce"]["to"] == channel.id:
# FOR DEBUGGING
# print("MEMBER LEAVE ANNOUNCEMENT CHANNEL IS REMOVED: " + str(channel))
pass
# FOR DEBUGGING
# print("!!!SERVER " + str(server) + " DATA HAS BEEN UPDATED!!!")
except Exception as e:
# FOR DEBUGGING
# print("!!!SOMETHING WENT WRONG WHILE TRYING TO UPDATE SERVERS DATA WITH THE " + str(channel) + " CHANNEL!!!\nERROR:\n" + str(e))
return
@bot.event
async def on_server_role_delete(role):
"""Whenever a server role is removed."""
try:
# FOR DEBUGGING
# print("Server Role Remove:\nRole: " + str(role))
# FOR DEBUGGING
# print("SERVER ID: " + str(role.server.id))
data = db.discordServerDictionary(role.server.id)
# FOR DEBUGGING
# print("DATA: " + str(data))
if data in [False, None]:
# FOR DEBUGGING
# print("!!!NO SERVER DATA!!!")
return
# Member joins events
if data["general"]["memberJoin"] == {}:
# FOR DEBUGGING
# print("!!!NO MEMBER JOINS EVENTS!!!")
pass
else:
if "giveRole" in data["general"]["memberJoin"]:
# FOR DEBUGGING
# print("!!!THERE IS GIVE ROLE TO MEMBER JOINING EVENT!!!")
# Check if it's the same id of the role that we give out
if data["general"]["memberJoin"]["giveRole"]["role"] == role.id:
# FOR DEBUGGING
# print("SPECIFIED ROLE HAS BEEN REMOVED: " + str(role))
if db.updateDiscordServer(role.server.id, {"general.memberJoin.giveRole": 1}, mode="$unset") == True:
await bot.send_message(role.server.owner, "Role, **" + str(role) + "**, given to new members has been removed from server, **" + str(role.server) + "**, so we won't be giving it to new members anymore... :eyes:")
else:
await bot.send_message(role.server.owner, "Role, **" + str(role) + "**, given to new member has been removed from server, **" + str(role.server) + "**, and while trying to fix this up an error occurred which means on role giving to new members might be broken... :confused:\nContact the developers here:\n__**" + str(config.bot_server) + "**__")
except Exception as e:
# FOR DEBUGGING
# print("!!!SOMETHING WENT WRONG WHILE TRYING TO UPDATE SERVER DATA WITH " + str(role).upper() + " ROLE!!!\nERROR:\n" + str(e))
return
if __name__ == "__main__":
"""Load and run the bot if this is the executed file"""
print("!!!LOADING EXTENSION COMMANDS!!!")
for extension in config.command_modules:
try: # Load extension commands in other command modules
bot.load_extension(extension)
print("!!!" + str(extension) + " HAS BEEN LOADED!!!")
except Exception as e: # If there is an error print it
print("!!!" + str(extension) + " WAS NOT LOADED!!!\nREASON:\n" + str(e) + "\n---\n" + str(format_exc()))
print("!!!ALL EXTENSION COMMANDS HAVE BEEN LOADED!!!")
bot.run(config.BOT_KEY) # Run the bot