diff --git a/plugins/lastfm.py b/plugins/lastfm.py index f35f738..628cbb4 100644 --- a/plugins/lastfm.py +++ b/plugins/lastfm.py @@ -109,15 +109,23 @@ def _current_meta(bot) -> tuple[str, str]: """Extract artist and title from the currently playing track. Returns (artist, title). Either or both may be empty. - Tries the music plugin's current track metadata, falling back to - splitting the title on common separators. + Tries the music plugin's current track metadata on this bot first, + then checks peer bots (shared registry) so extra bots can see what + the music bot is playing. """ - music_ps = bot._pstate.get("music", {}) - current = music_ps.get("current") - if current is None: - return ("", "") - raw_title = current.title or "" - return _parse_title(raw_title) + # Check this bot first, then peers + candidates = [bot] + for peer in getattr(getattr(bot, "registry", None), "_bots", {}).values(): + if peer is not bot: + candidates.append(peer) + for b in candidates: + music_ps = getattr(b, "_pstate", {}).get("music", {}) + current = music_ps.get("current") + if current is not None: + raw_title = current.title or "" + if raw_title: + return _parse_title(raw_title) + return ("", "") # -- Discovery orchestrator -------------------------------------------------- diff --git a/tests/test_lastfm.py b/tests/test_lastfm.py index 1c3ab10..193f882 100644 --- a/tests/test_lastfm.py +++ b/tests/test_lastfm.py @@ -307,6 +307,31 @@ class TestCurrentMeta: } assert _mod._current_meta(bot) == ("Tool", "Lateralus") + def test_peer_bot_music_state(self): + """Extra bot sees music state from peer bot via shared registry.""" + music_bot = _FakeBot() + music_bot._pstate["music"] = { + "current": _FakeTrack(title="Tool - Lateralus"), + } + extra_bot = _FakeBot() + # No music state on extra_bot + # Share registry with _bots index + shared_reg = _FakeRegistry() + shared_reg._bots = {"derp": music_bot, "merlin": extra_bot} + music_bot.registry = shared_reg + extra_bot.registry = shared_reg + assert _mod._current_meta(extra_bot) == ("Tool", "Lateralus") + + def test_peer_bot_no_music(self): + """Returns empty when no bot has music state.""" + bot_a = _FakeBot() + bot_b = _FakeBot() + shared_reg = _FakeRegistry() + shared_reg._bots = {"a": bot_a, "b": bot_b} + bot_a.registry = shared_reg + bot_b.registry = shared_reg + assert _mod._current_meta(bot_a) == ("", "") + # --------------------------------------------------------------------------- # TestFmtMatch