Compare commits

...

3 Commits

Author SHA1 Message Date
6803b130f7 Fixed bot being started multiple times. 2026-04-09 13:11:09 +02:00
ae0c707470 Threading actually sucks. 2026-04-09 13:04:20 +02:00
a17a790b61 Fixed circular import. 2026-04-09 12:28:06 +02:00
3 changed files with 38 additions and 21 deletions

7
bot_instance.py Normal file
View File

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

48
main.py
View File

@@ -1,21 +1,19 @@
import discord
import os
from discord.ext import tasks, commands
from discord.ext import tasks
from dotenv import load_dotenv
from dom5game import Dom5game
from servermanager import create_server
from nicegui import ui
from webui import create_ui
import threading
from bot_instance import bot
import asyncio
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
bot_started = False # prolly only need this in dev mode
load_dotenv()
TOKEN = os.getenv("TOKEN")
bot.tracked_games = []
bot.tracked_games_lock = threading.Lock()
if not TOKEN:
raise ValueError("No token found")
def reload_games():
@@ -29,12 +27,30 @@ def reload_games():
)
async def start_discord_bot():
await bot.load_extension("cogs.slash_commands")
await bot.start(TOKEN)
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)
ui.run()
if __name__ in {"__main__", "__mp_main__"}:
main()
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
await bot.load_extension("cogs.slash_commands")
# await bot.tree.sync()
reload_games()
if not task_loop.is_running():
task_loop.start()
@@ -48,8 +64,11 @@ async def task_loop():
)
for game in bot.tracked_games:
channel = bot.get_channel(game.channelId)
if channel is None:
continue
# 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()
pingstr = ""
if game.members:
@@ -93,12 +112,3 @@ async def task_loop():
)
game.update_players()
game.to_json()
def start_bot():
bot.run(TOKEN)
threading.Thread(target=start_bot, daemon=True).start()
create_ui()
ui.run()

View File

@@ -1,5 +1,5 @@
from nicegui import ui
from main import bot
from bot_instance import bot
# reminder:
# tracked_games etc erst in andere variable kopieren und lock benutzen.