fixed bot starting multiple times.

This commit is contained in:
2026-04-10 14:19:41 +02:00
parent 6803b130f7
commit 305ae44b66

20
main.py
View File

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