From a1839d18bf14ed4796587b111c77b01fa9b8c4a5 Mon Sep 17 00:00:00 2001 From: Elliot Williams <4396779+elliotwms@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:19:36 +0100 Subject: [PATCH] feat: Add message URL helpers --- endpoints.go | 5 +++++ message.go | 5 +++++ message_test.go | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/endpoints.go b/endpoints.go index 29189af69..27777fb40 100644 --- a/endpoints.go +++ b/endpoints.go @@ -162,6 +162,11 @@ var ( return EndpointMessageReactions(cID, mID, eID) + "/" + uID } + // EndpointMessageURL builds a canonical discord URL in the format https://discord.com/channels/:gID/:cID/:mID + EndpointMessageURL = func(gID, cID, mID string) string { + return EndpointDiscord + "channels/" + gID + "/" + cID + "/" + mID + } + EndpointPoll = func(cID, mID string) string { return EndpointChannel(cID) + "/polls/" + mID } diff --git a/message.go b/message.go index 16fedb36c..1b7eea738 100644 --- a/message.go +++ b/message.go @@ -595,3 +595,8 @@ type MessageInteractionMetadata struct { // NOTE: present only on modal submit interactions. TriggeringInteractionMetadata *MessageInteractionMetadata `json:"triggering_interaction_metadata,omitempty"` } + +// URL returns the canonical Discord message URL +func (m *Message) URL() string { + return EndpointMessageURL(m.GuildID, m.ChannelID, m.ID) +} diff --git a/message_test.go b/message_test.go index 270c4b8c3..d41010b71 100644 --- a/message_test.go +++ b/message_test.go @@ -50,3 +50,15 @@ func TestGettingEmojisFromMessage(t *testing.T) { } } + +func TestMessage_URL(t *testing.T) { + m := &Message{ + GuildID: "foo", + ChannelID: "bar", + ID: "baz", + } + + if m.URL() != "https://discord.com/channels/foo/bar/baz" { + t.Error("Unexpected url: " + m.URL()) + } +}