Compare commits
4 Commits
6803b130f7
...
35df0e8263
| Author | SHA1 | Date | |
|---|---|---|---|
| 35df0e8263 | |||
| c34c768dcb | |||
| aaff712a04 | |||
| 305ae44b66 |
@@ -1,7 +1,25 @@
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import discord
|
import discord
|
||||||
|
import threading
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
_started = False
|
||||||
|
|
||||||
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()
|
||||||
|
|
||||||
|
|
||||||
|
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
108
cogs/slash_commands.py
Normal 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))
|
||||||
30
main.py
30
main.py
@@ -1,20 +1,12 @@
|
|||||||
import discord
|
import discord
|
||||||
import os
|
import os
|
||||||
from discord.ext import tasks
|
from discord.ext import tasks
|
||||||
from dotenv import load_dotenv
|
|
||||||
from dom5game import Dom5game
|
from dom5game import Dom5game
|
||||||
from servermanager import create_server
|
|
||||||
from nicegui import ui
|
|
||||||
from webui import create_ui
|
from webui import create_ui
|
||||||
from bot_instance import bot
|
from bot_instance import bot, start_bot
|
||||||
|
from nicegui import ui
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
bot_started = False # prolly only need this in dev mode
|
|
||||||
load_dotenv()
|
|
||||||
TOKEN = os.getenv("TOKEN")
|
|
||||||
if not TOKEN:
|
|
||||||
raise ValueError("No token found")
|
|
||||||
|
|
||||||
|
|
||||||
def reload_games():
|
def reload_games():
|
||||||
with bot.tracked_games_lock:
|
with bot.tracked_games_lock:
|
||||||
@@ -27,34 +19,28 @@ def reload_games():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def start_discord_bot():
|
|
||||||
await bot.load_extension("cogs.slash_commands")
|
|
||||||
await bot.start(TOKEN)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global bot_started
|
|
||||||
create_ui()
|
create_ui()
|
||||||
if not bot_started:
|
loop = asyncio.get_event_loop()
|
||||||
bot_started = True
|
loop.create_task(start_bot())
|
||||||
ui.timer(0, lambda: asyncio.create_task(start_discord_bot()), once=True)
|
ui.run(reload=False)
|
||||||
ui.run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ in {"__main__", "__mp_main__"}:
|
if __name__ == "__main__":
|
||||||
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():
|
if not task_loop.is_running():
|
||||||
task_loop.start()
|
task_loop.start()
|
||||||
|
|
||||||
|
|
||||||
@tasks.loop(seconds=2)
|
@tasks.loop(seconds=5)
|
||||||
async def task_loop():
|
async def task_loop():
|
||||||
await bot.change_presence(
|
await bot.change_presence(
|
||||||
activity=discord.Activity(
|
activity=discord.Activity(
|
||||||
|
|||||||
26
webui.py
26
webui.py
@@ -1,17 +1,23 @@
|
|||||||
from nicegui import ui
|
from nicegui import ui
|
||||||
from bot_instance import bot
|
from bot_instance import bot
|
||||||
|
|
||||||
# reminder:
|
|
||||||
# tracked_games etc erst in andere variable kopieren und lock benutzen.
|
|
||||||
|
|
||||||
|
|
||||||
def create_ui():
|
def create_ui():
|
||||||
ui.label("Amogus")
|
pages = ui.sub_pages()
|
||||||
|
|
||||||
with bot.tracked_games_lock:
|
|
||||||
games = list(bot.tracked_games)
|
|
||||||
rows = []
|
rows = []
|
||||||
for game in games:
|
for game in bot.tracked_games:
|
||||||
rows.append({"Name": game.name})
|
pages.add(f"/{game.name}", lambda name=game.name: sub_page(name))
|
||||||
|
rows.append({"Name": game.name, "Turn": game.turn})
|
||||||
|
|
||||||
ui.table(rows=rows)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user