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": [
{
"enabled": false,
"stations": {
"end_stations": {
"general": "general.aac",
},
"stops": {
}
}
]
+1 -1
View File
@@ -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
+2 -1
View File
@@ -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:
+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)
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):
+7 -3
View File
@@ -122,8 +122,11 @@ 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
@@ -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: