Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(discord): name a thread from its message as people read it

raullopez-sandbox982d99ce9353
diff --git a/core/switch_core/bridges/collaboration/discord/adapter.py b/core/switch_core/bridges/collaboration/discord/adapter.py
index 537dee42f..2387beee8 100644
--- a/core/switch_core/bridges/collaboration/discord/adapter.py
+++ b/core/switch_core/bridges/collaboration/discord/adapter.py
@@ -3739,9 +3739,7 @@ class DiscordAdapter(PlatformAdapter):
root_message = None
try:
root_message = await channel.fetch_message(root_message_id)
- root_content = str(root_message.content or "").strip()
- if root_content:
- name = root_content[:60]
+ name = _thread_title(root_message) or name
except discord.HTTPException:
pass
@@ -3764,3 +3762,15 @@ class DiscordAdapter(PlatformAdapter):
logger.error("Invalid Discord message ref format: %s", message_ref)
return "", ""
return parts[0], parts[1]
+
+
+# A user, role or channel mention as Discord writes it in a message's text.
+_MENTION_TOKEN = re.compile(r"<(?:@[!&]?|#)\d+>")
+
+
+def _thread_title(message: Any) -> str:
+ """A thread's name from the message it starts at: its text with mentions
+ as people read them (`@name`, `#channel`), any Discord could not resolve
+ dropped, at most 60 characters."""
+ text = _MENTION_TOKEN.sub("", str(message.clean_content or ""))
+ return " ".join(text.split())[:60]
diff --git a/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py b/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py
index f92254e6f..7687b5541 100644
--- a/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py
+++ b/core/tests/switch_core/bridges/collaboration/test_discord_adapter.py
@@ -63,6 +63,11 @@ class _FakeMessage:
self.content = ""
self.threads_created: list[str] = []
+ @property
+ def clean_content(self) -> str:
+ # Discord's text with mentions resolved; these fakes carry none.
+ return self.content
+
async def edit(self, *, content: str) -> None:
self.edited = content
diff --git a/core/tests/switch_core/bridges/collaboration/test_discord_sdk_only.py b/core/tests/switch_core/bridges/collaboration/test_discord_sdk_only.py
index 277ba50eb..eaffcab2e 100644
--- a/core/tests/switch_core/bridges/collaboration/test_discord_sdk_only.py
+++ b/core/tests/switch_core/bridges/collaboration/test_discord_sdk_only.py
@@ -97,6 +97,11 @@ class _Message:
self.edits: list[dict[str, Any]] = []
self.deleted = False
+ @property
+ def clean_content(self) -> str:
+ # Discord's text with mentions resolved; these fakes carry none.
+ return self.content
+
async def edit(self, *, content: str, **kwargs: Any) -> None:
self.edited = content
self.edits.append({"content": content, **kwargs})
diff --git a/core/tests/switch_core/bridges/collaboration/test_discord_thread_title.py b/core/tests/switch_core/bridges/collaboration/test_discord_thread_title.py
new file mode 100644
index 000000000..68dcb7397
--- /dev/null
+++ b/core/tests/switch_core/bridges/collaboration/test_discord_thread_title.py
@@ -0,0 +1,17 @@
+"""A Discord thread made for a message is named from its text as people read
+it, never from the raw mention markup Discord stores."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+from switch_core.bridges.collaboration.discord.adapter import _thread_title
+
+
+def test_mentions_read_as_names_and_unresolved_ones_are_dropped() -> None:
+ resolved = SimpleNamespace(clean_content="@writer draft the launch notes")
+ assert _thread_title(resolved) == "@writer draft the launch notes"
+ unresolved = SimpleNamespace(clean_content="<@&123456> <#99> please look <@!42>")
+ assert _thread_title(unresolved) == "please look"
+ assert _thread_title(SimpleNamespace(clean_content="x" * 80)) == "x" * 60
+ assert _thread_title(SimpleNamespace(clean_content="<@&1>")) == ""