fix(discord): properly route slash event handling in threads

Cherry-picked from PR #2017 by @simpolism. Fixes #2011.

Discord slash commands in threads were missing thread_id in the
SessionSource, causing them to route to the parent channel session.
Commands like /usage and /reset returned wrong data or affected the
wrong session.

Detects discord.Thread channels in _build_slash_event and sets
chat_type='thread' with thread_id. Two tests added.
This commit is contained in:
Teknium 2026-03-22 04:25:19 -07:00
parent a9f9c60efd
commit ffa8b562e9
No known key found for this signature in database
2 changed files with 48 additions and 1 deletions

View file

@ -241,6 +241,42 @@ async def test_dispatch_thread_session_builds_thread_event(adapter):
assert "TestGuild" in event.source.chat_name
# ------------------------------------------------------------------
# _build_slash_event — preserve thread context for native slash commands
# ------------------------------------------------------------------
def test_build_slash_event_preserves_thread_context(adapter):
interaction = SimpleNamespace(
channel=_FakeThreadChannel(channel_id=555, name="Planning"),
channel_id=555,
user=SimpleNamespace(display_name="Jezza", id=42),
)
event = adapter._build_slash_event(interaction, "/status")
assert event.text == "/status"
assert event.source.chat_id == "555"
assert event.source.chat_type == "thread"
assert event.source.thread_id == "555"
assert "TestGuild" in event.source.chat_name
def test_build_slash_event_uses_group_context_for_channels(adapter):
interaction = SimpleNamespace(
channel=_FakeTextChannel(channel_id=123, name="general"),
channel_id=123,
user=SimpleNamespace(display_name="Jezza", id=42),
)
event = adapter._build_slash_event(interaction, "/status")
assert event.source.chat_id == "123"
assert event.source.chat_type == "group"
assert event.source.thread_id is None
assert "TestGuild / #general" == event.source.chat_name
# ------------------------------------------------------------------
# Auto-thread: _auto_create_thread
# ------------------------------------------------------------------