feat(MessageReference): add Type (#1595)

https://discord.com/developers/docs/resources/message#message-reference-types
This commit is contained in:
Elliot Williams 2025-03-04 09:44:35 +00:00 committed by GitHub
parent 33ffff21d3
commit 00db80a651
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 83 additions and 7 deletions

View file

@ -471,16 +471,28 @@ type MessageApplication struct {
Name string `json:"name"`
}
// MessageReferenceType is a type of MessageReference
type MessageReferenceType int
// Known valid MessageReferenceType values
// https://discord.com/developers/docs/resources/message#message-reference-types
const (
MessageReferenceTypeDefault MessageReferenceType = 0
MessageReferenceTypeForward MessageReferenceType = 1
)
// MessageReference contains reference data sent with crossposted messages
type MessageReference struct {
MessageID string `json:"message_id"`
ChannelID string `json:"channel_id,omitempty"`
GuildID string `json:"guild_id,omitempty"`
FailIfNotExists *bool `json:"fail_if_not_exists,omitempty"`
Type MessageReferenceType `json:"type,omitempty"`
MessageID string `json:"message_id"`
ChannelID string `json:"channel_id,omitempty"`
GuildID string `json:"guild_id,omitempty"`
FailIfNotExists *bool `json:"fail_if_not_exists,omitempty"`
}
func (m *Message) reference(failIfNotExists bool) *MessageReference {
func (m *Message) reference(refType MessageReferenceType, failIfNotExists bool) *MessageReference {
return &MessageReference{
Type: refType,
GuildID: m.GuildID,
ChannelID: m.ChannelID,
MessageID: m.ID,
@ -490,13 +502,18 @@ func (m *Message) reference(failIfNotExists bool) *MessageReference {
// Reference returns a MessageReference of the given message.
func (m *Message) Reference() *MessageReference {
return m.reference(true)
return m.reference(MessageReferenceTypeDefault, true)
}
// SoftReference returns a MessageReference of the given message.
// If the message doesn't exist it will instead be sent as a non-reply message.
func (m *Message) SoftReference() *MessageReference {
return m.reference(false)
return m.reference(MessageReferenceTypeDefault, false)
}
// Forward returns a MessageReference for a forwarded message.
func (m *Message) Forward() *MessageReference {
return m.reference(MessageReferenceTypeForward, true)
}
// ContentWithMentionsReplaced will replace all @<id> mentions with the

View file

@ -50,3 +50,62 @@ func TestGettingEmojisFromMessage(t *testing.T) {
}
}
func TestMessage_Reference(t *testing.T) {
m := &Message{
ID: "811736565172011001",
GuildID: "811736565172011002",
ChannelID: "811736565172011003",
}
ref := m.Reference()
if ref.Type != 0 {
t.Error("Default reference type should be 0")
}
if ref.MessageID != m.ID {
t.Error("Message ID should be the same")
}
if ref.GuildID != m.GuildID {
t.Error("Guild ID should be the same")
}
if ref.ChannelID != m.ChannelID {
t.Error("Channel ID should be the same")
}
}
func TestMessage_Forward(t *testing.T) {
m := &Message{
ID: "811736565172011001",
GuildID: "811736565172011002",
ChannelID: "811736565172011003",
}
ref := m.Forward()
if ref.Type != MessageReferenceTypeForward {
t.Error("Reference type should be 1 (forward)")
}
if ref.MessageID != m.ID {
t.Error("Message ID should be the same")
}
if ref.GuildID != m.GuildID {
t.Error("Guild ID should be the same")
}
if ref.ChannelID != m.ChannelID {
t.Error("Channel ID should be the same")
}
}
func TestMessageReference_DefaultTypeIsDefault(t *testing.T) {
r := MessageReference{}
if r.Type != MessageReferenceTypeDefault {
t.Error("Default message type should be MessageReferenceTypeDefault")
}
}