84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
# bots.py
|
|
import os
|
|
import json
|
|
import asyncio
|
|
import sys
|
|
import time
|
|
import traceback
|
|
from discord.ext import commands
|
|
from dotenv import load_dotenv
|
|
from bot_discord import DiscordBot
|
|
from bot_twitch import TwitchBot
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
# Load bot configuration
|
|
CONFIG_PATH = "config.json"
|
|
try:
|
|
with open(CONFIG_PATH, "r") as f:
|
|
config_data = json.load(f)
|
|
except FileNotFoundError:
|
|
print("Error: config.json not found.")
|
|
sys.exit(1)
|
|
except json.JSONDecodeError as e:
|
|
print(f"Error parsing config.json: {e}")
|
|
sys.exit(1)
|
|
|
|
# Global settings
|
|
bot_start_time = time.time()
|
|
|
|
###############################
|
|
# Simple Logging System
|
|
###############################
|
|
|
|
def log(message, level="INFO"):
|
|
"""
|
|
A simple logging function with adjustable log levels.
|
|
Logs messages in a structured format.
|
|
|
|
Available levels:
|
|
DEBUG, INFO, WARNING, ERROR, CRITICAL, FATAL
|
|
See 'config.json' for disabling/enabling logging levels
|
|
"""
|
|
log_levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "FATAL"]
|
|
if level not in log_levels:
|
|
level = "INFO" # Default to INFO if an invalid level is provided
|
|
|
|
if level in config_data["log_levels"]:
|
|
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
|
|
log_message = f"[{timestamp}] [{level}] {message}"
|
|
|
|
try:
|
|
print(log_message) # Print to terminal
|
|
except Exception:
|
|
pass # Prevent logging failures from crashing the bot
|
|
|
|
# Placeholder for future expansions (e.g., file logging, Discord alerts, etc.)
|
|
|
|
###############################
|
|
# Main Event Loop
|
|
###############################
|
|
|
|
async def main():
|
|
global discord_bot, twitch_bot
|
|
|
|
log("Initializing bots...", "INFO")
|
|
|
|
discord_bot = DiscordBot(config_data, log)
|
|
twitch_bot = TwitchBot(config_data, log)
|
|
|
|
log("Starting Discord and Twitch bots...", "INFO")
|
|
|
|
discord_task = asyncio.create_task(discord_bot.run(os.getenv("DISCORD_BOT_TOKEN")))
|
|
twitch_task = asyncio.create_task(twitch_bot.run())
|
|
|
|
await asyncio.gather(discord_task, twitch_task)
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
asyncio.run(main())
|
|
except Exception as e:
|
|
error_trace = traceback.format_exc()
|
|
log(f"Fatal Error: {e}\n{error_trace}", "FATAL")
|