OokamiPupV2/bots.py

108 lines
3.6 KiB
Python

# bots.py
import os
import json
import asyncio
import sys
import time
import traceback
import globals
from functools import partial
import twitchio.ext
from discord.ext import commands
from dotenv import load_dotenv
from bot_discord import DiscordBot
from bot_twitch import TwitchBot
#from modules.db import init_db_connection, run_db_operation
#from modules.db import ensure_quotes_table, ensure_users_table, ensure_chatlog_table, checkenable_db_fk
from modules import db, utility
# Load environment variables
load_dotenv()
# Clear previous current-run logfile
globals.reset_curlogfile()
# Load bot configuration
config_data = globals.Constants.config_data
###############################
# Main Event Loop
###############################
async def main():
global discord_bot, twitch_bot, db_conn
# Log initial start
globals.log("--------------- BOT STARTUP ---------------")
# Before creating your DiscordBot/TwitchBot, initialize DB
db_conn = globals.init_db_conn()
try: # Ensure FKs are enabled
db.checkenable_db_fk(db_conn)
except Exception as e:
globals.log(f"Unable to ensure Foreign keys are enabled: {e}", "WARNING")
# auto-create the quotes table if it doesn't exist
tables = {
"Bot events table": partial(db.ensure_bot_events_table, db_conn),
"Quotes table": partial(db.ensure_quotes_table, db_conn),
"Users table": partial(db.ensure_users_table, db_conn),
"Platform_Mapping table": partial(db.ensure_platform_mapping_table, db_conn),
"Chatlog table": partial(db.ensure_chatlog_table, db_conn),
"Howls table": partial(db.ensure_userhowls_table, db_conn),
"Discord activity table": partial(db.ensure_discord_activity_table, db_conn),
"Account linking table": partial(db.ensure_link_codes_table, db_conn),
"Community events table": partial(db.ensure_community_events_table, db_conn)
}
try:
for table, func in tables.items():
func() # Call the function with db_conn and log already provided
globals.log(f"{table} ensured.", "DEBUG")
except Exception as e:
globals.log(f"Unable to ensure DB tables exist: {e}", "FATAL")
globals.log("Initializing bots...")
# Create both bots
discord_bot = DiscordBot()
twitch_bot = TwitchBot()
# Log startup
utility.log_bot_startup(db_conn)
# Provide DB connection to both bots
try:
discord_bot.set_db_connection(db_conn)
twitch_bot.set_db_connection(db_conn)
globals.log(f"Initialized database connection to both bots")
except Exception as e:
globals.log(f"Unable to initialize database connection to one or both bots: {e}", "FATAL")
globals.log("Starting Discord and Twitch bots...")
discord_task = asyncio.create_task(discord_bot.run(os.getenv("DISCORD_BOT_TOKEN")))
twitch_task = asyncio.create_task(twitch_bot.run())
from modules.utility import dev_func
enable_dev_func = False
if enable_dev_func:
dev_func_result = dev_func(db_conn, enable_dev_func)
globals.log(f"dev_func output: {dev_func_result}")
await asyncio.gather(discord_task, twitch_task)
#await asyncio.gather(discord_task)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
utility.log_bot_shutdown(db_conn, intent="User Shutdown")
except Exception as e:
error_trace = traceback.format_exc()
globals.log(f"Fatal Error: {e}\n{error_trace}", "FATAL")
utility.log_bot_shutdown(db_conn)