transitous rewrite: voice_announcements on stops

This commit is contained in:
Kaaninchen
2026-08-18 11:41:15 +02:00
parent b608656bc7
commit 5886c747fe
5 changed files with 32 additions and 15 deletions
+3 -1
View File
@@ -24,8 +24,10 @@
"voice": [ "voice": [
{ {
"enabled": false, "enabled": false,
"stations": { "end_stations": {
"general": "general.aac", "general": "general.aac",
},
"stops": {
} }
} }
] ]
+1 -1
View File
@@ -25,7 +25,7 @@ async def on_ready():
logger(f"{bot.user} is online") logger(f"{bot.user} is online")
_bot_initialized = True _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_id = config.discord.server
server_vc_id = config.discord.vc server_vc_id = config.discord.vc
+2 -1
View File
@@ -23,7 +23,8 @@ class ConnectionsConfig:
@dataclass @dataclass
class VoiceAnnouncementConfig: class VoiceAnnouncementConfig:
enabled: bool enabled: bool
stations: dict[str, str] end_stations: dict[str, str]
stops: dict[str, str]
@dataclass @dataclass
class AnnouncementConfig: class AnnouncementConfig:
+18 -8
View File
@@ -38,7 +38,6 @@ async def rename_vc(bot: discord.Bot, voice_channel, from_scheduler: bool = Fals
formatting = channel_formatting(mode) formatting = channel_formatting(mode)
await voice_channel.edit(name=f"{formatting}{long_name}") await voice_channel.edit(name=f"{formatting}{long_name}")
await voice_channel.set_status(channel_lang.status())
start_next_stop_updates(bot, voice_channel) start_next_stop_updates(bot, voice_channel)
@@ -72,15 +71,22 @@ async def announcer(announcement: str, voice_channel: discord.VoiceChannel, dest
if embed: if embed:
await voice_channel.send(embed=embed) await voice_channel.send(embed=embed)
async def voice_announcer(destination: str, voice_channel: discord.VoiceChannel) -> bool: async def voice_announcer(destination: str, voice_channel: discord.VoiceChannel, type_announcement: str) -> bool:
sound_path = get_sound_path(destination=destination) sound_path = get_sound_path(destination=destination, type_announcement=type_announcement)
if sound_path is None: if sound_path is None:
return False 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}") 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) audio_source = discord.FFmpegPCMAudio(sound_path)
vc = await connect_task
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
if not vc.is_playing(): 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"] departure_dt = trip["departure_dt"]
if departure_dt > now: if departure_dt > now:
await bot.change_presence(activity=discord.Game(name="tschu tschu! • /info"))
wait_seconds = (departure_dt - now).total_seconds() wait_seconds = (departure_dt - now).total_seconds()
print(f"Waiting for {wait_seconds} seconds")
await asyncio.sleep(wait_seconds) await asyncio.sleep(wait_seconds)
while True: while True:
@@ -135,13 +139,19 @@ async def _update_next_loop(bot: discord.Bot, voice_channel: discord.VoiceChanne
if next_stop is None: if next_stop is None:
return return
presence_text = f"{lang.embeds.info.next_stop()}: {next_stop["name"]}" next_stop_str = next_stop["name"]
await voice_channel.set_status(presence_text)
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() wait_seconds = (next_stop["arrival"] - datetime.now(LOCAL_TZ)).total_seconds()
if wait_seconds > 0: if wait_seconds > 0:
await asyncio.sleep(wait_seconds) await asyncio.sleep(wait_seconds)
except asyncio.CancelledError: except asyncio.CancelledError:
await voice_channel.set_status(None)
raise raise
def start_next_stop_updates(bot: discord.bot, voice_channel: discord.VoiceChannel): def start_next_stop_updates(bot: discord.bot, voice_channel: discord.VoiceChannel):
+6 -2
View File
@@ -122,8 +122,11 @@ def get_operator_metadata(agency: str, route_color: str, mode: str) -> dict:
"slogans": slogans "slogans": slogans
} }
def get_sound_path(destination) -> str | None: def get_sound_path(destination, type_announcement: str) -> str | None:
voice_stations = config.announcements.voice[0].stations 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: if destination in voice_stations:
announcement_for = destination announcement_for = destination
@@ -157,6 +160,7 @@ def get_next_station(stops: dict, train_from: str) -> dict | None:
"name": name, "name": name,
"arrival": arrival_dt "arrival": arrival_dt
} }
return None return None
def format_via_list(stops: dict, via_and: str) -> str: def format_via_list(stops: dict, via_and: str) -> str: