EmperorFred/bot.py

91 lines
2.7 KiB
Python
Raw Permalink Normal View History

2025-12-06 02:21:47 +00:00
import asyncio
import argparse
from dotenv import load_dotenv
import discord
# Load environment variables
load_dotenv()
# Import bot classes and configs
from discord_bot.bot import EmperorFredDiscordBot # noqa: E402
from twitch_bot.bot import TwitchBot # noqa: E402
from discord_bot import config as dcfg # noqa: E402
from twitch_bot import config as tcfg # noqa: E402
from libs.RestartController import RestartController
async def run_discord_bot_loop() -> None:
controller = RestartController()
while True:
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
prefix = dcfg.DISCORD_COMMAND_PREFIX or "!"
bot = EmperorFredDiscordBot(
restart_controller=controller,
command_prefix=prefix,
intents=intents,
guild_id=dcfg.DISCORD_GUILD_ID,
)
print(f"Discord command prefix set to: '{prefix}'")
print("🚀 Starting Discord bot...")
try:
await bot.load_cogs()
await bot.start(dcfg.DISCORD_TOKEN)
finally:
# If a restart was requested by a command, loop and recreate the bot
if controller.consume():
print("🔁 Discord bot restart requested. Restarting...")
continue
break
async def run_twitch_bot_loop() -> None:
if not tcfg.TWITCH_TOKEN:
print("⚠️ TWITCH_TOKEN missing; Twitch bot will not start.")
return
controller = RestartController()
while True:
bot = TwitchBot(controller)
await bot.load_commands()
print("🚀 Starting Twitch bot...")
try:
# TwitchIO v3: start() will read token from env when not provided
await bot.start()
finally:
if controller.consume():
print("🔁 Twitch bot restart requested. Restarting...")
continue
break
async def main(which: str = "both"):
if which == "discord":
await run_discord_bot_loop()
return
if which == "twitch":
await run_twitch_bot_loop()
return
# Default: run both
await asyncio.gather(
run_discord_bot_loop(),
run_twitch_bot_loop(),
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run EmperorFred bots")
parser.add_argument(
"-b", "--bot",
choices=["discord", "twitch", "both"],
default="both",
help="Which bot to start (default: both)",
)
args = parser.parse_args()
try:
asyncio.run(main(args.bot))
except KeyboardInterrupt:
print("Shutting down...")