Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message_link/channel_link to MessageReference #1877

Merged
merged 10 commits into from
Apr 14, 2024
2 changes: 2 additions & 0 deletions changes/1877.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add `message_link` property to `MessageReference`
- Add `channel_link` property to `MessageReference`
22 changes: 22 additions & 0 deletions hikari/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,28 @@ class MessageReference:
a guild.
"""

@property
def message_link(self) -> typing.Optional[str]:
"""Generate a jump link to the referenced message.

This will be [`None`][] for channel follow add messages. This may
point to a deleted message.
"""
if self.id is None:
return None

guild_id_str = "@me" if self.guild_id is None else self.guild_id
return f"{urls.BASE_URL}/channels/{guild_id_str}/{self.channel_id}/{self.id}"

@property
def channel_link(self) -> str:
"""Generate a jump link to the channel the referenced message was sent in.

This will always be a valid link.
"""
guild_id_str = "@me" if self.guild_id is None else self.guild_id
return f"{urls.BASE_URL}/channels/{guild_id_str}/{self.channel_id}"


@attrs_extensions.with_copy
@attrs.define(hash=True, kw_only=True, weakref_slot=False)
Expand Down
23 changes: 23 additions & 0 deletions tests/hikari/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ def test_make_link_when_guild_is_none(self, message):
assert message.make_link(None) == "https://discord.com/channels/@me/456/789"


@pytest.fixture
def message_reference():
return messages.MessageReference(
app=None, guild_id=snowflakes.Snowflake(123), channel_id=snowflakes.Snowflake(456), id=snowflakes.Snowflake(789)
)


class TestMessageReference:
def test_make_link_when_guild_is_not_none(self, message_reference):
assert message_reference.message_link == "https://discord.com/channels/123/456/789"
assert message_reference.channel_link == "https://discord.com/channels/123/456"

def test_make_link_when_guild_is_none(self, message_reference):
message_reference.guild_id = None
assert message_reference.message_link == "https://discord.com/channels/@me/456/789"
assert message_reference.channel_link == "https://discord.com/channels/@me/456"

def test_make_link_when_id_is_none(self, message_reference):
message_reference.id = None
assert message_reference.message_link is None
assert message_reference.channel_link == "https://discord.com/channels/123/456"


@pytest.mark.asyncio
class TestAsyncMessage:
async def test_fetch_channel(self, message):
Expand Down
Loading