import discord import os from discord.ext import tasks from dom5game import Dom5game from webui import create_ui from bot_instance import bot, start_bot from nicegui import ui import asyncio 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) ) def main(): create_ui() loop = asyncio.get_event_loop() loop.create_task(start_bot()) ui.run(reload=False) if __name__ == "__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() @tasks.loop(seconds=5) 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) if channel is None: continue # handle turn ticks new_turn = game.get_turn() if new_turn != game.turn and new_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()