From 5886c747fec6fff3629bb83e2936c8a3f5d47a06 Mon Sep 17 00:00:00 2001 From: Kaaninchen <124433727+kaaninchen@users.noreply.github.com> Date: Tue, 18 Aug 2026 11:41:15 +0200 Subject: [PATCH] transitous rewrite: voice_announcements on stops --- config.json.example | 4 +++- main.py | 2 +- src/config.py | 3 ++- src/dc/handlers.py | 26 ++++++++++++++++++-------- src/utils.py | 12 ++++++++---- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/config.json.example b/config.json.example index 171972a..55612f7 100644 --- a/config.json.example +++ b/config.json.example @@ -24,8 +24,10 @@ "voice": [ { "enabled": false, - "stations": { + "end_stations": { "general": "general.aac", + }, + "stops": { } } ] diff --git a/main.py b/main.py index 367963a..305f751 100644 --- a/main.py +++ b/main.py @@ -25,7 +25,7 @@ async def on_ready(): logger(f"{bot.user} is online") _bot_initialized = True - bot.change_presence(activity=discord.Game(name="tschu tschu! • /info")) + await bot.change_presence(activity=discord.Game(name="tschu tschu! • /info")) server_id = config.discord.server server_vc_id = config.discord.vc diff --git a/src/config.py b/src/config.py index b6071e1..956e6d3 100644 --- a/src/config.py +++ b/src/config.py @@ -23,7 +23,8 @@ class ConnectionsConfig: @dataclass class VoiceAnnouncementConfig: enabled: bool - stations: dict[str, str] + end_stations: dict[str, str] + stops: dict[str, str] @dataclass class AnnouncementConfig: diff --git a/src/dc/handlers.py b/src/dc/handlers.py index e4b88b6..bd743c9 100644 --- a/src/dc/handlers.py +++ b/src/dc/handlers.py @@ -38,7 +38,6 @@ async def rename_vc(bot: discord.Bot, voice_channel, from_scheduler: bool = Fals formatting = channel_formatting(mode) await voice_channel.edit(name=f"{formatting}{long_name}") - await voice_channel.set_status(channel_lang.status()) start_next_stop_updates(bot, voice_channel) @@ -72,15 +71,22 @@ async def announcer(announcement: str, voice_channel: discord.VoiceChannel, dest if embed: await voice_channel.send(embed=embed) -async def voice_announcer(destination: str, voice_channel: discord.VoiceChannel) -> bool: - sound_path = get_sound_path(destination=destination) +async def voice_announcer(destination: str, voice_channel: discord.VoiceChannel, type_announcement: str) -> bool: + sound_path = get_sound_path(destination=destination, type_announcement=type_announcement) if sound_path is None: return False + + if voice_channel.guild.voice_client: + logger(f"Already in vc, skipping this announcement to be safe") + return False logger(f"Joining vc, playing {sound_path}") - vc = await voice_channel.connect(timeout=15, reconnect=True) + + connect_task = asyncio.create_task(voice_channel.connect(timeout=15, reconnect=True)) audio_source = discord.FFmpegPCMAudio(sound_path) + vc = await connect_task + loop = asyncio.get_running_loop() if not vc.is_playing(): @@ -124,9 +130,7 @@ async def _update_next_loop(bot: discord.Bot, voice_channel: discord.VoiceChanne departure_dt = trip["departure_dt"] if departure_dt > now: - await bot.change_presence(activity=discord.Game(name="tschu tschu! • /info")) wait_seconds = (departure_dt - now).total_seconds() - print(f"Waiting for {wait_seconds} seconds") await asyncio.sleep(wait_seconds) while True: @@ -135,13 +139,19 @@ async def _update_next_loop(bot: discord.Bot, voice_channel: discord.VoiceChanne if next_stop is None: return - presence_text = f"{lang.embeds.info.next_stop()}: {next_stop["name"]}" - await voice_channel.set_status(presence_text) + next_stop_str = next_stop["name"] + + status_text = f"{lang.embeds.info.next_stop()}: {next_stop_str}" + await voice_channel.set_status(status_text, reason="Next stop status") + + await voice_announcer(next_stop_str, voice_channel, type_announcement="stops") + wait_seconds = (next_stop["arrival"] - datetime.now(LOCAL_TZ)).total_seconds() if wait_seconds > 0: await asyncio.sleep(wait_seconds) except asyncio.CancelledError: + await voice_channel.set_status(None) raise def start_next_stop_updates(bot: discord.bot, voice_channel: discord.VoiceChannel): diff --git a/src/utils.py b/src/utils.py index f353d21..7274576 100644 --- a/src/utils.py +++ b/src/utils.py @@ -122,9 +122,12 @@ def get_operator_metadata(agency: str, route_color: str, mode: str) -> dict: "slogans": slogans } -def get_sound_path(destination) -> str | None: - voice_stations = config.announcements.voice[0].stations - +def get_sound_path(destination, type_announcement: str) -> str | None: + if type_announcement == "end_stations": + voice_stations = config.announcements.voice[0].end_stations + elif type_announcement == "stops": + voice_stations = config.announcements.voice[0].stops + if destination in voice_stations: announcement_for = destination else: @@ -156,7 +159,8 @@ def get_next_station(stops: dict, train_from: str) -> dict | None: return { "name": name, "arrival": arrival_dt - } + } + return None def format_via_list(stops: dict, via_and: str) -> str: