Skip to content

Commit

Permalink
Add message_link/channel_link to MessageReference (#1877)
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Developer <LeoDeveloper@protonmail.com>
Co-authored-by: beagold <86345081+beagold@users.noreply.github.com>
  • Loading branch information
Le0Developer and beagold authored Apr 14, 2024
1 parent 3a27bfd commit 9a7d58a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
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

0 comments on commit 9a7d58a

Please sign in to comment.