-
Notifications
You must be signed in to change notification settings - Fork 2
/
wingbot.py
70 lines (60 loc) · 1.86 KB
/
wingbot.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
import asyncio
import inspect
import os
import signal
import discord
import pymongo
from discord.ext import commands
from dotenv import load_dotenv
from utils.configManager import BotConfig
from utils.help import HelpCommand
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
MONGODB_CONNECTION_STRING = os.getenv("MONGODB_CONNECTION_STRING")
config = BotConfig()
bot = commands.Bot(command_prefix=config.commandPrefix, case_insensitive=True)
db_client = pymongo.MongoClient(MONGODB_CONNECTION_STRING)
bot.config = config
bot.db_client = db_client
async def signal_handler():
"""
Signal handler to perform required cleanup operations before quitting bot
"""
print("Cleaning up the mess...")
for item in config.cogs:
cog = bot.get_cog(item.capitalize())
print(f"Executing signal handlers of the {item} cog...")
if hasattr(cog, "signal_handler"):
if inspect.iscoroutinefunction(cog.signal_handler):
await cog.signal_handler()
else:
cog.signal_handler()
db_client.close()
await bot.close()
@bot.event
async def on_ready():
"""
Finish set up once bot is ready
"""
print("Logged in as")
print(bot.user.name)
print(bot.user.id)
print("------")
await bot.change_presence(activity=discord.Game(name=config.activity))
cogs = config.cogs
cog_directory = config.cogDirectory
for cog in cogs:
try:
bot.load_extension(cog_directory + "." + cog)
except commands.errors.ExtensionAlreadyLoaded:
pass
loop = asyncio.get_event_loop()
for signame in ("SIGINT", "SIGTERM"):
loop.add_signal_handler(
getattr(signal, signame), lambda: asyncio.ensure_future(signal_handler())
)
return
# Adding custom help command
helpCommand = HelpCommand()
bot.help_command = helpCommand
bot.run(TOKEN)