Skip to content

The agent that grows with you

fix(discord): normalize channel links in home destinations

funky-xamarind3dc76718e95
diff --git a/gateway/config.py b/gateway/config.py
index 93d3f342971..5d5d3324725 100644
--- a/gateway/config.py
+++ b/gateway/config.py
@@ -312,6 +312,20 @@ class HomeChannel:
user_id: Optional[str] = None
scope_id: Optional[str] = None
+ def __post_init__(self) -> None:
+ # Copy Link is next to Copy Channel ID in Discord. Normalize at the
+ # shared home boundary so env, YAML and plugin-seeded homes agree.
+ if self.platform != Platform.DISCORD or not isinstance(self.chat_id, str):
+ return
+ import re
+ match = re.fullmatch(
+ r"https://(?:(?:ptb|canary)\.)?discord(?:app)?\.com/channels/"
+ r"(?:[0-9]+|@me)/([0-9]+)/?",
+ self.chat_id.strip(),
+ )
+ if match:
+ self.chat_id = match.group(1)
+
def to_dict(self) -> Dict[str, Any]:
optional = {k: v for k in ("thread_id", "user_id", "scope_id") if (v := getattr(self, k))}
return {"platform": self.platform.value, "chat_id": self.chat_id, "name": self.name, **optional}
diff --git a/tests/gateway/test_discord_home_link.py b/tests/gateway/test_discord_home_link.py
new file mode 100644
index 00000000000..51440fb4689
--- /dev/null
+++ b/tests/gateway/test_discord_home_link.py
@@ -0,0 +1,43 @@
+"""Discord home links must be normalized before any delivery consumer sees them."""
+import pytest
+from gateway.config import HomeChannel, Platform, load_gateway_config
+
+
[email protected]("source", ["env", "yaml"])
+def test_discord_home_link_loads_as_channel_id(tmp_path, monkeypatch, source):
+ channel_id = "202523857364451329"
+ link = f"https://discord.com/channels/302523857364451329/{channel_id}"
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.delenv("DISCORD_HOME_CHANNEL", raising=False)
+ config_text = "platforms:\n discord:\n enabled: false\n"
+ if source == "env":
+ monkeypatch.setenv("DISCORD_HOME_CHANNEL", link)
+ else:
+ config_text += f" home_channel:\n platform: discord\n chat_id: '{link}'\n"
+ (tmp_path / "config.yaml").write_text(config_text, encoding="utf-8")
+ home = load_gateway_config().platforms[Platform.DISCORD].home_channel
+ assert home.chat_id == channel_id
+
+
[email protected]("host", ["discord.com", "ptb.discord.com", "canary.discord.com", "discordapp.com"])
+def test_home_link_preserves_metadata_and_roundtrips(host):
+ home = HomeChannel(Platform.DISCORD, f" https://{host}/channels/@me/123456789/ ",
+ "Home", thread_id="987", user_id="u", scope_id="s")
+ assert home.chat_id == "123456789"
+ assert HomeChannel.from_dict(home.to_dict()) == home
+ assert (home.thread_id, home.user_id, home.scope_id) == ("987", "u", "s")
+
+
[email protected]("target", [
+ "123456789", "https://example.com/channels/123/456",
+ "https://discord.com.evil.test/channels/123/456",
+ "https://discord.com/channels/123/456/789", # message link, not channel
+ "https://discord.com/channels/123/not-a-channel", "123/456",
+])
+def test_unrecognized_targets_are_not_reinterpreted(target):
+ assert HomeChannel(Platform.DISCORD, target, "Home").chat_id == target
+
+
+def test_other_platform_home_is_unchanged():
+ target = "https://discord.com/channels/123/456"
+ assert HomeChannel(Platform.SLACK, target, "Home").chat_id == target