ttstoy: keep accessibility voice picks sticky across VC bounce
This commit is contained in:
+44
-19
@@ -397,7 +397,12 @@ class TtsToy(Cog):
|
|||||||
self.tts_locks = {} # guild_id -> asyncio.Lock
|
self.tts_locks = {} # guild_id -> asyncio.Lock
|
||||||
self.tts_processors = {} # guild_id -> asyncio.Task
|
self.tts_processors = {} # guild_id -> asyncio.Task
|
||||||
self._access_last_speaker = {} # guild_id -> last author id spoken (accessibility auto-read)
|
self._access_last_speaker = {} # guild_id -> last author id spoken (accessibility auto-read)
|
||||||
self._access_user_voice = {} # guild_id -> {user_id: voice} (in-memory only, not persisted)
|
# Per-user accessibility voices, in-memory only (never persisted).
|
||||||
|
# Overrides (explicit user picks) are STICKY: they survive VC flicker and
|
||||||
|
# engine-neutral resets, and only clear on start/stop. Auto-assignments
|
||||||
|
# are disposable and get rebuilt freely.
|
||||||
|
self._access_user_override = {} # guild_id -> {user_id: voice} explicit picks
|
||||||
|
self._access_auto_voice = {} # guild_id -> {user_id: voice} auto-assigned
|
||||||
self._access_assign_idx = {} # guild_id -> next round-robin index into the voice pool
|
self._access_assign_idx = {} # guild_id -> next round-robin index into the voice pool
|
||||||
|
|
||||||
self._ensure_sfx_folders()
|
self._ensure_sfx_folders()
|
||||||
@@ -2076,7 +2081,8 @@ class TtsToy(Cog):
|
|||||||
engine = await self.config.accessibility_engine()
|
engine = await self.config.accessibility_engine()
|
||||||
# Fresh session: clear any lingering per-user voice assignments.
|
# Fresh session: clear any lingering per-user voice assignments.
|
||||||
if ctx.guild:
|
if ctx.guild:
|
||||||
self._access_user_voice.pop(ctx.guild.id, None)
|
self._access_user_override.pop(ctx.guild.id, None)
|
||||||
|
self._access_auto_voice.pop(ctx.guild.id, None)
|
||||||
self._access_assign_idx.pop(ctx.guild.id, None)
|
self._access_assign_idx.pop(ctx.guild.id, None)
|
||||||
self._access_last_speaker.pop(ctx.guild.id, None)
|
self._access_last_speaker.pop(ctx.guild.id, None)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
@@ -2095,7 +2101,8 @@ class TtsToy(Cog):
|
|||||||
await self.config.accessibility_mode.set(False)
|
await self.config.accessibility_mode.set(False)
|
||||||
# Drop all in-memory per-user voice assignments (never persisted anyway).
|
# Drop all in-memory per-user voice assignments (never persisted anyway).
|
||||||
if ctx.guild:
|
if ctx.guild:
|
||||||
self._access_user_voice.pop(ctx.guild.id, None)
|
self._access_user_override.pop(ctx.guild.id, None)
|
||||||
|
self._access_auto_voice.pop(ctx.guild.id, None)
|
||||||
self._access_assign_idx.pop(ctx.guild.id, None)
|
self._access_assign_idx.pop(ctx.guild.id, None)
|
||||||
self._access_last_speaker.pop(ctx.guild.id, None)
|
self._access_last_speaker.pop(ctx.guild.id, None)
|
||||||
await ctx.send("**Accessibility mode stopped.** Normal TTS settings restored.")
|
await ctx.send("**Accessibility mode stopped.** Normal TTS settings restored.")
|
||||||
@@ -2118,9 +2125,11 @@ class TtsToy(Cog):
|
|||||||
return await ctx.send("Engine must be `dectalk` or `piper`.")
|
return await ctx.send("Engine must be `dectalk` or `piper`.")
|
||||||
await self.config.accessibility_engine.set(engine)
|
await self.config.accessibility_engine.set(engine)
|
||||||
if ctx.guild:
|
if ctx.guild:
|
||||||
self._access_user_voice.pop(ctx.guild.id, None)
|
# Clear only auto-assignments; explicit overrides persist and are
|
||||||
|
# re-validated against the new engine's pool on next resolve.
|
||||||
|
self._access_auto_voice.pop(ctx.guild.id, None)
|
||||||
self._access_assign_idx.pop(ctx.guild.id, None)
|
self._access_assign_idx.pop(ctx.guild.id, None)
|
||||||
await ctx.send(f"Accessibility engine set to `{engine}`. Per-user voices reset.")
|
await ctx.send(f"Accessibility engine set to `{engine}`. Auto-assigned voices reset; your own picks are kept if valid for `{engine}`.")
|
||||||
|
|
||||||
@accessibility_group.command(name="voice")
|
@accessibility_group.command(name="voice")
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
@@ -2150,9 +2159,10 @@ class TtsToy(Cog):
|
|||||||
)
|
)
|
||||||
|
|
||||||
choice = voice.strip()
|
choice = voice.strip()
|
||||||
gmap = self._access_user_voice.setdefault(ctx.guild.id, {})
|
|
||||||
if choice.lower() == "auto":
|
if choice.lower() == "auto":
|
||||||
gmap.pop(ctx.author.id, None)
|
self._access_user_override.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
||||||
|
# Drop any stale auto-assignment too so a fresh distinct one is picked.
|
||||||
|
self._access_auto_voice.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
||||||
new_voice = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
new_voice = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
||||||
return await ctx.send(f"Your accessibility voice is now auto-assigned: `{new_voice}`.")
|
return await ctx.send(f"Your accessibility voice is now auto-assigned: `{new_voice}`.")
|
||||||
|
|
||||||
@@ -2161,7 +2171,9 @@ class TtsToy(Cog):
|
|||||||
if match is None:
|
if match is None:
|
||||||
listing = ", ".join(f"`{v}`" for v in pool)
|
listing = ", ".join(f"`{v}`" for v in pool)
|
||||||
return await ctx.send(f"`{choice}` is not a `{engine}` voice. Available: {listing}")
|
return await ctx.send(f"`{choice}` is not a `{engine}` voice. Available: {listing}")
|
||||||
gmap[ctx.author.id] = match
|
self._access_user_override.setdefault(ctx.guild.id, {})[ctx.author.id] = match
|
||||||
|
# Clear any auto-assignment so the override is unambiguous.
|
||||||
|
self._access_auto_voice.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
||||||
await ctx.send(f"Your accessibility voice is now `{match}` (not saved between sessions).")
|
await ctx.send(f"Your accessibility voice is now `{match}` (not saved between sessions).")
|
||||||
|
|
||||||
@ttstoy_group.command(name="piperurl")
|
@ttstoy_group.command(name="piperurl")
|
||||||
@@ -2710,21 +2722,31 @@ class TtsToy(Cog):
|
|||||||
def _access_voice_for(self, guild_id: int, user_id: int, engine: str) -> str:
|
def _access_voice_for(self, guild_id: int, user_id: int, engine: str) -> str:
|
||||||
"""Resolve a speaker's accessibility voice.
|
"""Resolve a speaker's accessibility voice.
|
||||||
|
|
||||||
Returns an explicit per-user override if one is set, otherwise
|
An explicit override (set via the `voice` command) always wins and is
|
||||||
auto-assigns the next voice from the engine's pool (round-robin, so
|
sticky: it survives VC flicker and engine-neutral resets. Otherwise the
|
||||||
concurrent speakers get distinct voices). Assignments live in memory
|
user gets an auto-assigned voice from the engine's pool (round-robin, so
|
||||||
only and are not persisted between sessions.
|
concurrent speakers get distinct voices). Both maps live in memory only
|
||||||
|
and are not persisted between sessions.
|
||||||
"""
|
"""
|
||||||
pool = self._access_voice_pool(engine)
|
pool = self._access_voice_pool(engine)
|
||||||
if not pool:
|
if not pool:
|
||||||
return None
|
return None
|
||||||
gmap = self._access_user_voice.setdefault(guild_id, {})
|
|
||||||
existing = gmap.get(user_id)
|
# 1) Explicit override wins, but only if valid for the current engine's
|
||||||
# Keep an existing assignment only if it is valid for the current engine.
|
# pool (a dectalk pick must not bleed into piper mode).
|
||||||
|
override = self._access_user_override.get(guild_id, {}).get(user_id)
|
||||||
|
if override in pool:
|
||||||
|
return override
|
||||||
|
|
||||||
|
# 2) Keep an existing auto-assignment if still valid for this engine.
|
||||||
|
auto = self._access_auto_voice.setdefault(guild_id, {})
|
||||||
|
existing = auto.get(user_id)
|
||||||
if existing in pool:
|
if existing in pool:
|
||||||
return existing
|
return existing
|
||||||
# Prefer a voice not already handed out in this guild, round-robin.
|
|
||||||
used = set(gmap.values())
|
# 3) Assign a new voice, avoiding every voice already in use in this
|
||||||
|
# guild (both overrides and auto-assignments), round-robin.
|
||||||
|
used = set(auto.values()) | set(self._access_user_override.get(guild_id, {}).values())
|
||||||
idx = self._access_assign_idx.get(guild_id, 0)
|
idx = self._access_assign_idx.get(guild_id, 0)
|
||||||
chosen = None
|
chosen = None
|
||||||
for _ in range(len(pool)):
|
for _ in range(len(pool)):
|
||||||
@@ -2737,7 +2759,7 @@ class TtsToy(Cog):
|
|||||||
chosen = pool[self._access_assign_idx.get(guild_id, 0) % len(pool)]
|
chosen = pool[self._access_assign_idx.get(guild_id, 0) % len(pool)]
|
||||||
idx = self._access_assign_idx.get(guild_id, 0) + 1
|
idx = self._access_assign_idx.get(guild_id, 0) + 1
|
||||||
self._access_assign_idx[guild_id] = idx
|
self._access_assign_idx[guild_id] = idx
|
||||||
gmap[user_id] = chosen
|
auto[user_id] = chosen
|
||||||
return chosen
|
return chosen
|
||||||
|
|
||||||
def _access_describe_message(self, message: discord.Message) -> Optional[str]:
|
def _access_describe_message(self, message: discord.Message) -> Optional[str]:
|
||||||
@@ -2875,7 +2897,10 @@ class TtsToy(Cog):
|
|||||||
return
|
return
|
||||||
if before.channel != after.channel and member.guild:
|
if before.channel != after.channel and member.guild:
|
||||||
self._access_last_speaker.pop(member.guild.id, None)
|
self._access_last_speaker.pop(member.guild.id, None)
|
||||||
self._access_user_voice.pop(member.guild.id, None)
|
# Only auto-assignments reset on a VC bounce; explicit user
|
||||||
|
# picks are sticky so a mid-session reconnect (e.g. the bot
|
||||||
|
# being pulled out to show a mute notice) does not wipe them.
|
||||||
|
self._access_auto_voice.pop(member.guild.id, None)
|
||||||
self._access_assign_idx.pop(member.guild.id, None)
|
self._access_assign_idx.pop(member.guild.id, None)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception("[Accessibility] voice reset error")
|
log.exception("[Accessibility] voice reset error")
|
||||||
|
|||||||
Reference in New Issue
Block a user