From c2fbc09b16bb47f8ce74f3f24fd835f2497f0fcb Mon Sep 17 00:00:00 2001 From: kingston Date: Sun, 18 Oct 2026 22:33:30 -0500 Subject: [PATCH] ttstoy: accessibility mode auto-reads VC side chat When accessibility mode is on and the bot is in a voice channel, every new message in that VC's integrated text chat is spoken via DECTalk with no command prefix needed. - Prefix 'Name said:' only on a user's first message since another user last spoke (interrupted), tracked per guild in memory and reset when the bot changes voice channel. - Attachments/embeds/links are announced, not read: 'username sent a link' / 'sent an image' / 'sent a file'. - Strips all emoji (unicode and custom) from spoken content. No emoji, no em-dash in any output. - Skips bot messages and valid command invocations. --- ttstoy/ttstoy.py | 139 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 4 deletions(-) diff --git a/ttstoy/ttstoy.py b/ttstoy/ttstoy.py index c14d4c1..cc8fc5f 100644 --- a/ttstoy/ttstoy.py +++ b/ttstoy/ttstoy.py @@ -142,6 +142,9 @@ VOICE_SWITCH_PATTERN = re.compile( VALID_VOICE_MODES = {"minimax", "chatterbox", "dectalk", "morshu", "vox"} +# Pattern for detecting a URL/link anywhere in message text (accessibility auto-read) +URL_PATTERN = re.compile(r"https?://\S+", re.IGNORECASE) + def resolve_voice_for_mode(mode: str, voice_name: str, chatterbox_voices: list = None) -> str: """Resolve a voice name/label to the engine-specific voice ID, case-insensitive. @@ -380,6 +383,7 @@ class TtsToy(Cog): self.tts_queues = {} # guild_id -> asyncio.Queue self.tts_locks = {} # guild_id -> asyncio.Lock self.tts_processors = {} # guild_id -> asyncio.Task + self._access_last_speaker = {} # guild_id -> last author id spoken (accessibility auto-read) self._ensure_sfx_folders() self.clear_old_tts.start() @@ -1936,12 +1940,13 @@ class TtsToy(Cog): if new_state: await ctx.send( - "♿ **Accessibility mode enabled.**\n" - "• TTS forced to DECTalk\n" - "• Emoji SFX disabled (emojis read as text instead)" + "**Accessibility mode enabled.**\n" + "- TTS forced to DECTalk\n" + "- Emoji SFX disabled (emojis read as text instead)\n" + "- Auto-reads the side chat of the voice channel the bot is in (no command prefix needed)" ) else: - await ctx.send("♿ **Accessibility mode disabled.** Normal TTS settings restored.") + await ctx.send("**Accessibility mode disabled.** Normal TTS settings restored.") @ttstoy_group.command(name="voxpack") @commands.is_owner() @@ -2421,6 +2426,132 @@ class TtsToy(Cog): ) return self.tts_queues[guild_id] + # --------------------------------------------------------------------- + # ACCESSIBILITY AUTO-READ (VC side-chat, no command prefix) + # --------------------------------------------------------------------- + def _access_describe_message(self, message: discord.Message) -> Optional[str]: + """Build the spoken text for an accessibility auto-read of a VC side-chat message. + + Returns the raw content to speak, or None if there is nothing to read. + Attachments/embeds/links are announced instead of read: + " sent a link" / "sent an image" / "sent a file" + No emoji, no em-dash. + """ + name = message.author.display_name + + # Attachments take priority: classify image vs generic file + if message.attachments: + att = message.attachments[0] + ctype = (att.content_type or "").lower() + fname = (att.filename or "").lower() + is_image = ctype.startswith("image/") or fname.endswith( + (".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".heic") + ) + if is_image: + return f"{name} sent an image" + return f"{name} sent a file" + + # A link in the text (or a link-only embed) is announced, not read + if URL_PATTERN.search(message.content or "") or message.embeds: + return f"{name} sent a link" + + content = (message.content or "").strip() + if not content: + return None + # No emoji anywhere: strip unicode emoji and custom/named emoji tokens. + content = re.sub( + r'[\U0001F300-\U0001FAFF\U00002600-\U000026FF\U00002700-\U000027BF]', + '', content + ) + content = re.sub(r'', '', content) # custom <:name:id> + content = content.strip() + if not content: + return None + return content + + @commands.Cog.listener("on_message") + async def _accessibility_autoread(self, message: discord.Message): + try: + if message.author.bot or not message.guild: + return + + if not await self.config.accessibility_mode(): + return + + guild = message.guild + + # Bot must be connected to a voice channel, and the message must be + # in that voice channel's integrated (side) chat. + if not guild.me.voice or not guild.me.voice.channel: + return + vc = guild.me.voice.channel + if message.channel.id != vc.id: + return + + # Ignore our own command invocations so [p]tts etc. are not double-read. + ctx = await self.bot.get_context(message) + if ctx.valid: + return + + spoken = self._access_describe_message(message) + if not spoken: + return + + # Prefix the speaker's name only on their first message since someone + # else last spoke (i.e. they were "interrupted"). + last = self._access_last_speaker.get(guild.id) + if last != message.author.id: + spoken = f"{message.author.display_name} said: {spoken}" + self._access_last_speaker[guild.id] = message.author.id + + dectalk_url = await self._get_dectalk_api_url() + sfx_volume = await self.config.sfx_volume() + + self.tts_storage.mkdir(parents=True, exist_ok=True) + audio_path = str(self.tts_storage.joinpath(f"access-{message.id}.mp3")) + + try: + await asyncio.to_thread( + self._save_prompt_audio, + None, None, None, spoken, audio_path, sfx_volume, + "dectalk", dectalk_url, "vox", None, None, None, 0.0, None, + ) + except Exception: + log.exception("[Accessibility] Failed to generate DECTalk audio") + return + + if not os.path.exists(audio_path): + return + + _channel = message.channel + _author = message.author + _guild = guild + + class _AccessCtx: + guild = _guild + channel = _channel + author = _author + interaction = None + async def react_quietly(self, *a, **kw): + pass + + queue = self._get_or_create_tts_queue(guild.id) + await queue.put((_AccessCtx(), audio_path)) + except Exception: + log.exception("[Accessibility] auto-read listener error") + + @commands.Cog.listener("on_voice_state_update") + async def _accessibility_voice_reset(self, member, before, after): + # Reset the accessibility last-speaker when the BOT joins, leaves, or + # moves voice channels, so a stale "interrupted" state does not carry over. + try: + if member.id != self.bot.user.id: + return + if before.channel != after.channel and member.guild: + self._access_last_speaker.pop(member.guild.id, None) + except Exception: + log.exception("[Accessibility] voice reset error") + # --------------------------------------------------------------------- # SPEAK COMMAND (JUST [p]tts) # ---------------------------------------------------------------------