Skip to content

The agent that grows with you

fix(discord): native slash commands honor guild/channel profile_routes

Teknium0437fe66f774
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index a98b016bfc5..1839d57dd01 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -6456,6 +6456,14 @@ class DiscordAdapter(BasePlatformAdapter):
)
return (len(self._skill_entries), self._skill_group_hidden_count)
+ def _interaction_guild_id(self, interaction: discord.Interaction) -> Optional[str]:
+ """Resolve the guild id of a slash interaction (mirrors the message path)."""
+ guild_id = getattr(interaction, "guild_id", None)
+ if guild_id is None:
+ guild = getattr(getattr(interaction, "channel", None), "guild", None)
+ guild_id = getattr(guild, "id", None)
+ return str(guild_id) if guild_id else None
+
def _build_slash_event(self, interaction: discord.Interaction, text: str) -> MessageEvent:
"""Build a MessageEvent from a Discord slash command interaction."""
is_dm = isinstance(interaction.channel, discord.DMChannel)
@@ -6480,6 +6488,12 @@ class DiscordAdapter(BasePlatformAdapter):
# For forum threads, inherit the parent forum's topic.
chat_topic = self._get_effective_topic(interaction.channel, is_thread=is_thread)
+ # guild_id/parent_chat_id feed profile_routes matching in build_source,
+ # exactly as on_message passes them — without them a guild- or
+ # channel-routed profile never matches a native slash command (#69178).
+ parent_id = (
+ self._get_parent_channel_id(interaction.channel) if is_thread else None
+ ) or ""
source = self.build_source(
chat_id=str(interaction.channel_id),
chat_name=chat_name,
@@ -6488,11 +6502,12 @@ class DiscordAdapter(BasePlatformAdapter):
user_name=interaction.user.display_name,
thread_id=thread_id,
chat_topic=chat_topic,
+ guild_id=self._interaction_guild_id(interaction),
+ parent_chat_id=parent_id or None,
)
msg_type = MessageType.COMMAND if text.startswith("/") else MessageType.TEXT
channel_id = str(interaction.channel_id)
- parent_id = str(getattr(getattr(interaction, "channel", None), "parent_id", "") or "")
return MessageEvent(
text=text,
message_type=msg_type,
@@ -6574,6 +6589,8 @@ class DiscordAdapter(BasePlatformAdapter):
_chan = getattr(interaction, "channel", None)
chat_topic = self._get_effective_topic(_chan, is_thread=True) if _chan else None
+ _parent_channel = self._thread_parent_channel(getattr(interaction, "channel", None))
+ _parent_id = str(getattr(_parent_channel, "id", "") or "")
source = self.build_source(
chat_id=thread_id,
chat_name=chat_name,
@@ -6582,10 +6599,10 @@ class DiscordAdapter(BasePlatformAdapter):
user_name=interaction.user.display_name,
thread_id=thread_id,
chat_topic=chat_topic,
+ guild_id=self._interaction_guild_id(interaction),
+ parent_chat_id=_parent_id or None,
)
- _parent_channel = self._thread_parent_channel(getattr(interaction, "channel", None))
- _parent_id = str(getattr(_parent_channel, "id", "") or "")
_skills = self._resolve_channel_skills(thread_id, _parent_id or None)
_channel_prompt = self._resolve_channel_prompt(thread_id, _parent_id or None)
event = MessageEvent(
diff --git a/tests/gateway/test_discord_slash_commands.py b/tests/gateway/test_discord_slash_commands.py
index e3e5a39ff57..4f602a8768d 100644
--- a/tests/gateway/test_discord_slash_commands.py
+++ b/tests/gateway/test_discord_slash_commands.py
@@ -602,3 +602,43 @@ def test_register_skill_command_payload_fits_discord_8kb_limit(adapter):
)
+
+
+# ------------------------------------------------------------------
+# _build_slash_event — guild/parent ids reach profile_routes (#69178, #91633)
+# ------------------------------------------------------------------
+
+
+def test_build_slash_event_routes_guild_profile_like_messages(adapter, monkeypatch):
+ """A guild-keyed profile route must match a native slash command exactly
+ as it matches a regular message: build_source needs guild_id (and the
+ thread's parent_chat_id) or the route never fires and /new resets the
+ default profile's session instead of the routed one."""
+ from gateway import run as gateway_run
+ from gateway.config import GatewayConfig
+ from gateway.profile_routing import ProfileRoute
+
+ runner = object.__new__(gateway_run.GatewayRunner)
+ runner.config = GatewayConfig(
+ multiplex_profiles=True,
+ profile_routes=[ProfileRoute(name="work", profile="work", platform="discord", guild_id="1")],
+ )
+ monkeypatch.setattr(gateway_run, "_multiplex_profile_homes", lambda _cfg: [("work", None)])
+ adapter.gateway_runner = runner
+ user = SimpleNamespace(display_name="Jezza", id=42)
+
+ channel_event = adapter._build_slash_event(
+ SimpleNamespace(channel=SimpleNamespace(id=200, name="general", guild=SimpleNamespace(id=1, name="G"), topic=None),
+ channel_id=200, guild_id=1, user=user),
+ "/new",
+ )
+ thread_event = adapter._build_slash_event(
+ SimpleNamespace(channel=_FakeThreadChannel(channel_id=555), channel_id=555, guild_id=None, user=user),
+ "/status",
+ )
+
+ assert channel_event.source.guild_id == "1"
+ assert channel_event.source.profile == "work"
+ assert thread_event.source.guild_id == "1"
+ assert thread_event.source.parent_chat_id == "100"
+ assert thread_event.source.profile == "work"