Threading actually sucks.

This commit is contained in:
2026-04-09 13:04:20 +02:00
parent a17a790b61
commit ae0c707470
2 changed files with 25 additions and 16 deletions

View File

@@ -1,9 +1,7 @@
from discord.ext import commands from discord.ext import commands
import discord import discord
import threading
intents = discord.Intents.default() intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents) bot = commands.Bot(command_prefix="!", intents=intents)
bot.tracked_games = [] bot.tracked_games = []
bot.tracked_games_lock = threading.Lock()

37
main.py
View File

@@ -1,16 +1,18 @@
import discord import discord
import os import os
from discord.ext import tasks, commands from discord.ext import tasks
from dotenv import load_dotenv from dotenv import load_dotenv
from dom5game import Dom5game from dom5game import Dom5game
from servermanager import create_server from servermanager import create_server
from nicegui import ui from nicegui import ui
from webui import create_ui from webui import create_ui
import threading
from bot_instance import bot from bot_instance import bot
import asyncio
load_dotenv() load_dotenv()
TOKEN = os.getenv("TOKEN") TOKEN = os.getenv("TOKEN")
if not TOKEN:
raise ValueError("No token found")
def reload_games(): def reload_games():
@@ -24,12 +26,27 @@ def reload_games():
) )
async def start_discord_bot():
await bot.load_extension("cogs.slash_commands")
await bot.start(TOKEN)
def main():
create_ui()
ui.timer(0, lambda: asyncio.create_task(start_discord_bot()), once=True)
ui.run()
if __name__ in {"__main__", "__mp_main__"}:
main()
@bot.event @bot.event
async def on_ready(): async def on_ready():
print(f"Logged in as {bot.user}") print(f"Logged in as {bot.user}")
await bot.load_extension("cogs.slash_commands")
# await bot.tree.sync() # await bot.tree.sync()
reload_games() reload_games()
if not task_loop.is_running():
task_loop.start() task_loop.start()
@@ -43,8 +60,11 @@ async def task_loop():
) )
for game in bot.tracked_games: for game in bot.tracked_games:
channel = bot.get_channel(game.channelId) channel = bot.get_channel(game.channelId)
if channel is None:
continue
# handle turn ticks # handle turn ticks
if game.get_turn() != game.turn and game.get_turn().isdigit(): new_turn = game.get_turn()
if new_turn != game.turn and new_turn.isdigit():
game.update_turn() game.update_turn()
pingstr = "" pingstr = ""
if game.members: if game.members:
@@ -88,12 +108,3 @@ async def task_loop():
) )
game.update_players() game.update_players()
game.to_json() game.to_json()
def start_bot():
bot.run(TOKEN)
threading.Thread(target=start_bot, daemon=True).start()
create_ui()
ui.run()