Compare commits

...

9 Commits

Author SHA1 Message Date
35df0e8263 Forgor. 2026-04-11 01:54:08 +02:00
c34c768dcb Changed everything, gonna clean up later. 2026-04-11 01:53:46 +02:00
aaff712a04 Changed details command to exclude AI. 2026-04-10 14:49:57 +02:00
305ae44b66 fixed bot starting multiple times. 2026-04-10 14:19:41 +02:00
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
d74534965c Added more experimental ui stuff and renamed ui.py to webui.py. 2026-04-09 12:24:41 +02:00
1980716d3b Basic ui implementation. 2026-04-09 11:58:15 +02:00
5 changed files with 223 additions and 25 deletions

25
bot_instance.py Normal file
View File

@@ -0,0 +1,25 @@
from discord.ext import commands
import discord
import threading
import os
from dotenv import load_dotenv
_started = False
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
bot.tracked_games = []
bot.tracked_games_lock = threading.Lock()
async def start_bot():
global _started
if _started:
return
_started = True
load_dotenv()
TOKEN = os.getenv("TOKEN")
if not TOKEN:
raise ValueError("No token found")
await bot.start(TOKEN)

108
cogs/slash_commands.py Normal file
View File

@@ -0,0 +1,108 @@
import discord
from discord import app_commands
from discord.ext import commands
from dom5game import Dom5game
class SlashCommands(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
def game_autocomplete(self, current):
options = []
for game in self.bot.tracked_games:
options.append(game.name)
return [
app_commands.Choice(name=option, value=option)
for option in options
if option.lower().startswith(current.lower())
][:25]
@app_commands.command(name="ping", description="Check bot latency")
async def ping(self, interaction: discord.Interaction):
await interaction.response.send_message(
f"Pong! {round(self.bot.latency * 1000)}ms"
)
@app_commands.command(
name="dom5-addgame",
description="Adds a game that is already running but not tracked by the bot yet.",
)
async def addgame(self, interaction: discord.Interaction, name: str):
try:
if any(game.name == name for game in self.bot.tracked_games):
await interaction.response.send_message("Game already tracked.")
return
game = Dom5game(name, interaction.channel_id, [], 0, {})
game.update_turn()
game.update_players()
game.to_json()
self.bot.tracked_games.append(game)
await interaction.response.send_message("Added the game.")
except:
await interaction.response.send_message(
"Something went wrong. Are you sure the name is correct and the game exists?"
)
@app_commands.command(name="dom5-creategame", description="Creates a new game.")
async def creategame(self, interaction: discord.Interaction, name: str, port: int):
print("a")
@app_commands.command(
name="domt5-pingme",
description="Signs you up to be pinged for a game. Run the command again to not get pinged anymore.",
)
async def pingme(self, interaction: discord.Interaction, name: str):
game = Dom5game.get_game_by_name(name, self.bot.tracked_games)
if game == None:
await interaction.response.send_message("Game does not exist.")
return
try:
if interaction.user.id in game.members:
game.members.remove(interaction.user.id)
await interaction.response.send_message(
'You will no longer receive turn notifications for game: **"'
+ name
+ '"**.'
)
else:
game.members.append(interaction.user.id)
await interaction.response.send_message(
'You will now receive turn notifications for game: **"'
+ name
+ '"**.'
)
except:
await interaction.response.send_message("Something went wrong.")
@pingme.autocomplete("name")
async def pingme_autocomplete(self, interaction: discord.Interaction, current: str):
return self.game_autocomplete(current)
@app_commands.command(
name="dom5-details", description="Shows the details of an ongoing game."
)
async def details(self, interaction: discord.Interaction, name: str):
game = Dom5game.get_game_by_name(name, self.bot.tracked_games)
if game == None:
await interaction.response.send_message("Game does not exist.")
return
embed = discord.Embed(title=name)
for player in game.players.keys():
if game.players[player] not in {"AI", "Eliminated"}:
embed.add_field(name=player, value=game.players[player])
await interaction.response.send_message(embed=embed)
@details.autocomplete("name")
async def details_autocomplete(
self, interaction: discord.Interaction, current: str
):
return self.game_autocomplete(current)
async def setup(bot: commands.Bot):
await bot.add_cog(SlashCommands(bot))

38
main.py
View File

@@ -1,19 +1,15 @@
import discord
import os
from discord.ext import tasks, commands
from dotenv import load_dotenv
from discord.ext import tasks
from dom5game import Dom5game
from servermanager import create_server
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
load_dotenv()
TOKEN = os.getenv("TOKEN")
bot.tracked_games = []
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:
@@ -23,8 +19,15 @@ def reload_games():
)
create_server("Amogus", 7777, 831955362646851617)
reload_games()
def main():
create_ui()
loop = asyncio.get_event_loop()
loop.create_task(start_bot())
ui.run(reload=False)
if __name__ == "__main__":
main()
@bot.event
@@ -33,10 +36,11 @@ async def on_ready():
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=2)
@tasks.loop(seconds=5)
async def task_loop():
await bot.change_presence(
activity=discord.Activity(
@@ -46,8 +50,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:
@@ -91,6 +98,3 @@ async def task_loop():
)
game.update_players()
game.to_json()
bot.run(TOKEN)

View File

@@ -1,18 +1,56 @@
aiofiles==25.1.0
aiohappyeyeballs==2.6.1
aiohttp==3.13.5
aiosignal==1.4.0
annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.13.0
attrs==26.1.0
audioop-lts==0.2.2
bidict==0.23.1
certifi==2026.2.25
click==8.3.2
discord==2.3.2
discord.py==2.7.1
docutils==0.22.4
dotenv==0.9.9
fastapi==0.135.3
frozenlist==1.8.0
h11==0.16.0
httpcore==1.0.9
httptools==0.7.1
httpx==0.28.1
idna==3.11
ifaddr==0.2.0
itsdangerous==2.2.0
Jinja2==3.1.6
lxml==6.0.2
lxml_html_clean==0.4.4
markdown2==2.5.5
MarkupSafe==3.0.3
multidict==6.7.1
nicegui==3.10.0
numpy==2.4.4
orjson==3.11.8
pandas==3.0.2
propcache==0.4.1
pydantic==2.12.5
pydantic_core==2.41.5
Pygments==2.20.0
python-dateutil==2.9.0.post0
python-dotenv==1.2.2
python-engineio==4.13.1
python-multipart==0.0.24
python-socketio==5.16.1
PyYAML==6.0.3
simple-websocket==1.1.0
six==1.17.0
starlette==1.0.0
typing-inspection==0.4.2
typing_extensions==4.15.0
uvicorn==0.44.0
uvloop==0.22.1
watchfiles==1.1.1
websockets==16.0
wsproto==1.3.2
yarl==1.23.0

23
webui.py Normal file
View File

@@ -0,0 +1,23 @@
from nicegui import ui
from bot_instance import bot
def create_ui():
pages = ui.sub_pages()
rows = []
for game in bot.tracked_games:
pages.add(f"/{game.name}", lambda name=game.name: sub_page(name))
rows.append({"Name": game.name, "Turn": game.turn})
pages.add("/", lambda: main_page(rows))
ui.run(reload=False)
def main_page(rows):
ui.label("test")
ui.table(rows=rows, title="Currently Running Games")
def sub_page(name: str):
ui.label(name)