Skip to content

Commit

Permalink
add make_link() to MessageReference
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0Developer committed Apr 8, 2024
1 parent 7dfa9ba commit e8cd84f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hikari/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,18 @@ class MessageReference:
a guild.
"""

def make_link(self) -> str:
"""Generate a jump link to this message or channel in case for follow add messages.
Returns
-------
str
The jump link to the message.
"""
guild_id_str = "@me" if self.guild_id is None else str(self.guild_id)
message_id_str = "" if self.id is None else str(self.id)
return f"{urls.BASE_URL}/channels/{guild_id_str}/{self.channel_id}/{message_id_str}"


@attrs_extensions.with_copy
@attrs.define(hash=True, kw_only=True, weakref_slot=False)
Expand Down
20 changes: 20 additions & 0 deletions tests/hikari/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ 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.make_link() == "https://discord.com/channels/123/456/789"

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

def test_make_link_when_id_is_none(self, message_reference):
message_reference.id = None
assert message_reference.make_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 e8cd84f

Please sign in to comment.