Compare commits
3 Commits
d74534965c
...
6803b130f7
| Author | SHA1 | Date | |
|---|---|---|---|
| 6803b130f7 | |||
| ae0c707470 | |||
| a17a790b61 |
7
bot_instance.py
Normal file
7
bot_instance.py
Normal 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
48
main.py
@@ -1,21 +1,19 @@
|
|||||||
import discord
|
import discord
|
||||||
import os
|
import os
|
||||||
from discord.ext import tasks, commands
|
from discord.ext import tasks
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from dom5game import Dom5game
|
from dom5game import Dom5game
|
||||||
from servermanager import create_server
|
from servermanager import create_server
|
||||||
from nicegui import ui
|
from nicegui import ui
|
||||||
from webui import create_ui
|
from webui import create_ui
|
||||||
import threading
|
from bot_instance import bot
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
bot_started = False # prolly only need this in dev mode
|
||||||
intents = discord.Intents.default()
|
|
||||||
bot = commands.Bot(command_prefix="!", intents=intents)
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
TOKEN = os.getenv("TOKEN")
|
TOKEN = os.getenv("TOKEN")
|
||||||
|
if not TOKEN:
|
||||||
bot.tracked_games = []
|
raise ValueError("No token found")
|
||||||
bot.tracked_games_lock = threading.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def reload_games():
|
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
|
@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():
|
||||||
task_loop.start()
|
task_loop.start()
|
||||||
|
|
||||||
|
|
||||||
@@ -48,8 +64,11 @@ async def task_loop():
|
|||||||
)
|
)
|
||||||
for game in bot.tracked_games:
|
for game in bot.tracked_games:
|
||||||
channel = bot.get_channel(game.channelId)
|
channel = bot.get_channel(game.channelId)
|
||||||
|
if channel is None:
|
||||||
|
continue
|
||||||
# handle turn ticks
|
# 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()
|
game.update_turn()
|
||||||
pingstr = ""
|
pingstr = ""
|
||||||
if game.members:
|
if game.members:
|
||||||
@@ -93,12 +112,3 @@ async def task_loop():
|
|||||||
)
|
)
|
||||||
game.update_players()
|
game.update_players()
|
||||||
game.to_json()
|
game.to_json()
|
||||||
|
|
||||||
|
|
||||||
def start_bot():
|
|
||||||
bot.run(TOKEN)
|
|
||||||
|
|
||||||
|
|
||||||
threading.Thread(target=start_bot, daemon=True).start()
|
|
||||||
create_ui()
|
|
||||||
ui.run()
|
|
||||||
|
|||||||
2
webui.py
2
webui.py
@@ -1,5 +1,5 @@
|
|||||||
from nicegui import ui
|
from nicegui import ui
|
||||||
from main import bot
|
from bot_instance import bot
|
||||||
|
|
||||||
# reminder:
|
# reminder:
|
||||||
# tracked_games etc erst in andere variable kopieren und lock benutzen.
|
# tracked_games etc erst in andere variable kopieren und lock benutzen.
|
||||||
|
|||||||
Reference in New Issue
Block a user