-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot_Main.py
More file actions
119 lines (89 loc) · 3.46 KB
/
Bot_Main.py
File metadata and controls
119 lines (89 loc) · 3.46 KB
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
import logging
import os
import discord
import mongoengine
from discord.ext import commands
from Azure.Load_Azure import load_azure
from Command_Cogs.custom_commands import handle_msg
from Mango.database_interface import add_exp, Users, Guilds
bot_token = os.environ.get('BOT_TOKEN')
def connect():
print(' Connecting to MongoDB...')
db_username = os.environ.get('DB_USERNAME')
db_password = os.environ.get('DB_PASSWORD')
db_host = os.environ.get('DB_HOST')
db_name = os.environ.get('DB_NAME')
mongoengine.connect(db_name,
username=db_username,
password=db_password,
host=db_host)
print(' Done.\n\n Loading Azure:')
# TODO Uncomment azure
load_azure()
return mongoengine.connection
bot = commands.Bot(description="Ironic Bot by Perfect_Irony#5196", command_prefix="$", pm_help=False)
@bot.event
async def on_message(message):
if await handle_msg(message) != True:
await bot.process_commands(message)
await add_exp(message, bot)
@bot.event
async def on_ready():
print('Logged in as ' + str(bot.user.name) + ' (ID:' + str(bot.user.id) + ') | Connected to '
+ str(len(bot.guilds)) + ' servers | Connected to ' + str(len(set(bot.get_all_members())))
+ ' users')
print('Loading users to DB...')
if Users.objects.count() < len(set(bot.get_all_members())):
for member in bot.get_all_members():
try:
Users(user_id=int(member.id), exp=0).save()
except mongoengine.errors.OperationError as e:
pass
except Exception as e:
print(e)
else:
print(' No new users, skipping.')
print('Loading guilds to DB...')
if Guilds.objects.count() < len(set(bot.guilds)):
for guild in bot.guilds:
try:
id = guild.id
Guilds(guild_id=id, custom_commands=[]).save()
print(' Added: ' + guild.name)
except mongoengine.errors.OperationError as e:
print(' ' + str(e))
print(' This error accured when adding this server: ' + guild.name + ' id:' + str(guild.id))
except Exception as e:
print(e)
else:
print(' No new guilds, skipping.')
print('Done.')
print('Loading guild settings...')
for guild in bot.guilds:
pass
print('Done.')
print("\nBot is now ready.")
return await bot.change_presence(activity=discord.Game('with bits | $versioninfo'))
# Run the client using the built-in client.run. Clear restart is redundant, but keeping it just in case.
def run_client(*args, **kwargs):
while True:
print('\nConnecting to databases...')
mongo_connection = connect()
print("\n\nDone loading databases.\n")
# TODO Uncomment logging
logging.basicConfig(level=logging.INFO)
initial_extensions = ['Command_Cogs.Fun',
'Command_Cogs.Misc',
'Command_Cogs.Moderation']
for extension in initial_extensions:
try:
bot.load_extension(extension)
except Exception as e:
print(f'Failed to load extension {extension}. error: ' + str(e))
bot.load_extension('Bot_Events.Misc_Events')
try:
bot.run(bot_token)
finally:
bot.clear()
print('restarting')
run_client()