mirror of
https://github.com/kaaninchen/Gleiswechsel.git
synced 2026-09-17 16:52:47 +00:00
Zeitschleife anhand von Echtzeit Ankunftsuhrzeit hinzugefügt
Ein recht großer commit. Mit dabei ist ein logger, erweiterte Informationen zu den Zügen und eine Zeitschleife, welche sich anhand der echtzeit ankunftsdaten des Zuges richtet. Der Commit benötigt auf jeden fall testing
This commit is contained in:
+41
-12
@@ -1,35 +1,64 @@
|
||||
import discord
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
import asyncio
|
||||
from src.config import config
|
||||
from src.utils import random_connection
|
||||
from src.utils import random_connection, get_train_info, logger
|
||||
|
||||
current = None
|
||||
_scheduled_task: asyncio.Task | None = None
|
||||
|
||||
async def rename_vc(bot: discord.Bot) -> bool:
|
||||
global current, train_name
|
||||
async def rename_vc(bot: discord.Bot, scheduled = False) -> bool:
|
||||
global current, train_name, train_info, _scheduled_task
|
||||
|
||||
guild = bot.get_guild(int(config["server"]))
|
||||
if guild is None:
|
||||
print(f"Es konnte kein Server mit der ID {config['server']} gefunden werden! Ist der Bot ein Member?")
|
||||
logger(f"Es konnte kein Server mit der ID {config['server']} gefunden werden! Ist der Bot ein Member?")
|
||||
return False
|
||||
|
||||
channel = guild.get_channel(int(config["vc"]))
|
||||
if not isinstance(channel, discord.VoiceChannel):
|
||||
print(f"Es konnte kein VC mit der ID {config['vc']} auf dem Server gefunden werden")
|
||||
logger(f"Es konnte kein VC mit der ID {config['vc']} auf dem Server gefunden werden")
|
||||
return False
|
||||
|
||||
current = random_connection()
|
||||
train_type = current['train'].split()[0]
|
||||
if train_type in ("IC", "ICE", "NJ"):
|
||||
if train_type in ("IC", "ICE", "NJ", "ES"):
|
||||
train = current['train']
|
||||
train_ID = current['train'].split()[1]
|
||||
else:
|
||||
train = current['train'].split()[1]
|
||||
train_ID = current['train_number']
|
||||
|
||||
train_info = get_train_info(station=current['station'], train_ID=train_ID, train_type=train_type)
|
||||
train_name = f"{train} nach {current['destination']}"
|
||||
|
||||
if not scheduled and _scheduled_task and not _scheduled_task.done():
|
||||
_scheduled_task.cancel()
|
||||
|
||||
if train_info:
|
||||
_scheduled_task = asyncio.create_task(_schedule_next_umstieg(bot, train_info["arrival"]))
|
||||
|
||||
print("-----------------------------------------")
|
||||
current_time = time.strftime('%X')
|
||||
print(f"{current_time}: Umstieg: {train_name} (Typ {train_type}) von {current['station']}")
|
||||
print(f"{current_time}: Wenn hier keine Nachricht mehr kommt bin ich im Cooldown")
|
||||
logger(f"Umstieg: {train_name} (Typ {train_type}) von {current['station']}")
|
||||
logger(f"Wenn der Name nicht geändert wird bin ich im Cooldown")
|
||||
await channel.edit(name=f"{config['formatting']}{train_name}",)
|
||||
print(f"{current_time}: Name geändert!")
|
||||
return True
|
||||
logger(f"Name geändert!")
|
||||
|
||||
return True
|
||||
|
||||
async def _schedule_next_umstieg(bot, arrival_iso):
|
||||
try:
|
||||
arrival = datetime.fromisoformat(arrival_iso)
|
||||
# wait_seconds = (arrival - datetime.now()).total_seconds()
|
||||
wait_seconds = 30
|
||||
|
||||
if wait_seconds > 0:
|
||||
remaining = str(timedelta(seconds=wait_seconds))
|
||||
logger(f"In {remaining} gehts weiter!...")
|
||||
await asyncio.sleep(wait_seconds)
|
||||
logger("Zug angekommen, wähle neue Verbindung...")
|
||||
await rename_vc(bot, scheduled=True)
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger("Geplanter Umstieg wurde abgebrochen (manueller /umstieg dazwischen oder CTRL + C)")
|
||||
raise
|
||||
Reference in New Issue
Block a user