Files
dom5bot/main.py
2026-04-09 12:28:06 +02:00

100 lines
2.8 KiB
Python

import discord
import os
from discord.ext import tasks, commands
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
load_dotenv()
TOKEN = os.getenv("TOKEN")
def reload_games():
with bot.tracked_games_lock:
bot.tracked_games = []
for _, _, files in os.walk("games"):
for name in files:
if name.endswith("json"):
bot.tracked_games.append(
Dom5game.load_json("games/" + name[:-5] + "/" + name)
)
@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()
task_loop.start()
@tasks.loop(seconds=2)
async def task_loop():
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name=str(len(bot.tracked_games)) + " Servers.",
)
)
for game in bot.tracked_games:
channel = bot.get_channel(game.channelId)
# handle turn ticks
if game.get_turn() != game.turn and game.get_turn().isdigit():
game.update_turn()
pingstr = ""
if game.members:
pingstr += "-# "
for member in game.members:
pingstr += "<@" + str(member) + "> "
await channel.send(
'Game **"'
+ game.name
+ '"** has advanced to turn '
+ game.turn
+ " \n"
+ pingstr
)
game.to_json()
# handle uploaded turns
turns = game.get_new_turns()
if turns != {}:
for key in turns:
value = turns[key]
if value == "Turn played":
await channel.send(
'Game **"'
+ game.name
+ '"**: '
+ key
+ " has played their turn."
)
if value == "Eliminated":
await channel.send(
'Game **"' + game.name + '"**: ' + key + " has been eliminated."
)
if value == "Turn unfinished":
await channel.send(
'Game **"'
+ game.name
+ '"**: '
+ key
+ " has marked their turn as unfinished."
)
game.update_players()
game.to_json()
def start_bot():
bot.run(TOKEN)
threading.Thread(target=start_bot, daemon=True).start()
create_ui()
ui.run()