-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTitanBot.py
120 lines (93 loc) · 3.81 KB
/
TitanBot.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
import asyncio
import concurrent.futures
import os
import subprocess
import sys
import threading
import time
import discord
from discord.ext import commands
from Framework.CommandGroups import custom_commands_loader
from Framework.CommandGroups.CurseForge import CurseForge
from Framework.CommandGroups.Debugging import Debugging
from Framework.CommandGroups.Fun import Fun
from Framework.CommandGroups.Genius import Genius
from Framework.CommandGroups.Quotes import Quotes
from Framework.CommandGroups.Statistics import Statistics
from Framework.CommandGroups.Utility import Utility
from Framework.ConfigurationManager import BotStatus, ConfigurationValues
from Framework.ConfigurationManager import configuration_manager
from Framework.GeneralUtilities import ErrorHandler
from Framework.GeneralUtilities.ThreadedLogger import ThreadedLogger
from Framework.IPC import ipc_handler
def start_watch_for_shutdown(loop: asyncio.AbstractEventLoop):
asyncio.set_event_loop(loop)
loop.run_until_complete(watch_for_shutdown())
shutdown_loop = asyncio.new_event_loop()
executor = concurrent.futures.ThreadPoolExecutor()
if __name__ == "__main__":
ConfigurationValues.VERSION = "v3.1.0"
ConfigurationValues.COMMIT = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
intents = discord.Intents.all()
bot = discord.Bot(intents=intents)
ThreadedLogger.initialize()
logger = ThreadedLogger("TitanBot")
logger.log_info("TitanBot " + ConfigurationValues.VERSION + " @ " + ConfigurationValues.COMMIT + " starting up")
logger.log_info("Running on Python " + str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2]))
cogs = [
Quotes(bot, configuration_manager),
Fun(bot, configuration_manager),
Utility(bot, configuration_manager),
Genius(bot, configuration_manager),
CurseForge(bot, configuration_manager),
Debugging(bot, configuration_manager),
Statistics(bot, configuration_manager)
]
for cog in cogs:
bot.add_cog(cog)
# Load custom user cogs from the CustomCommands folder
custom_commands_loader.load_custom_cogs(bot, configuration_manager)
# Purge the temporary file directory
path = f"{os.getcwd()}/Storage/Temp"
temp_file_count = 0
try:
for file in os.listdir(path):
logger.log_debug(f"Removing temporary file: {file}")
os.remove(f"{path}/{file}")
temp_file_count += 1
logger.log_info(f"Removed {temp_file_count} temporary files")
except FileNotFoundError:
# Create the directory
os.makedirs(path)
@bot.event
async def on_ready():
logger.log_info("TitanBot has connected to Discord")
await bot.change_presence(activity=discord.CustomActivity(name="Initializing..."), status=discord.Status.dnd)
# Update local configurations
logger.log_info("Loading configuration data...")
configuration_manager.bot = bot
await configuration_manager.load_deferred_configs(bot.guilds)
# Perform post-init tasks for each cog
for cog in cogs:
await cog.post_init()
# Set the bot status
status = await BotStatus.get_status_from_config(configuration_manager)
await bot.change_presence(activity=status[0], status=status[1])
# Start the IPC server
threading.Thread(target=ipc_handler.start_server, args=[bot], daemon=True).start()
executor.submit(start_watch_for_shutdown, shutdown_loop)
logger.log_info("TitanBot is ready to go!")
@bot.event
async def on_application_command_error(ctx: discord.ApplicationContext, error: commands.CommandError):
embed = await ErrorHandler.handle_error(ctx, error, logger)
await ctx.respond(embed=embed)
async def watch_for_shutdown():
# Monitor for shutdown events over IPC
while True:
if ipc_handler.shutdown_flag.is_set():
ThreadedLogger.shutdown = True
await cogs[5].check.stop() # CF update checker
await bot.close()
break
time.sleep(1)
bot.run(ConfigurationValues.TOKEN)