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 MessageReferenceType #1595

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}