diff --git a/main.py b/main.py index ae1bc1c..78cdf88 100644 --- a/main.py +++ b/main.py @@ -8,8 +8,10 @@ from nicegui import ui from webui import create_ui from bot_instance import bot import asyncio +import threading -bot_started = False # prolly only need this in dev mode +bot_start_lock = threading.Lock() +bot_started = False load_dotenv() TOKEN = os.getenv("TOKEN") if not TOKEN: @@ -35,9 +37,11 @@ async def start_discord_bot(): def main(): global bot_started create_ui() - if not bot_started: - bot_started = True - ui.timer(0, lambda: asyncio.create_task(start_discord_bot()), once=True) + with bot_start_lock: + if not bot_started and __name__ == "__mp_main__": + print("Starting bot") + bot_started = True + ui.timer(0, lambda: asyncio.create_task(start_discord_bot()), once=True) ui.run() @@ -54,15 +58,17 @@ async def on_ready(): task_loop.start() -@tasks.loop(seconds=2) +@tasks.loop(seconds=5) async def task_loop(): + with bot.tracked_games_lock: + games = list(bot.tracked_games) await bot.change_presence( activity=discord.Activity( type=discord.ActivityType.watching, - name=str(len(bot.tracked_games)) + " Servers.", + name=str(len(games)) + " Servers.", ) ) - for game in bot.tracked_games: + for game in bot.games: channel = bot.get_channel(game.channelId) if channel is None: continue