diff --git a/endpoints.go b/endpoints.go index c79cfe6..25c6141 100644 --- a/endpoints.go +++ b/endpoints.go @@ -92,6 +92,10 @@ var ( EndpointWebhook = func(wID string) string { return EndpointWebhooks + wID } EndpointWebhookToken = func(wID, token string) string { return EndpointWebhooks + wID + "/" + token } + EndpointMessageReactions = func(cID, mID, eID string) string { + return EndpointChannelMessage(cID, mID) + "/reactions/" + eID + "/@me" + } + EndpointInvite = func(iID string) string { return EndpointAPI + "invite/" + iID } EndpointIntegrationsJoin = func(iID string) string { return EndpointAPI + "integrations/" + iID + "/join" } diff --git a/events.go b/events.go index 49f12ad..c5404ec 100644 --- a/events.go +++ b/events.go @@ -35,6 +35,8 @@ var eventToInterface = map[string]interface{}{ "MESSAGE_CREATE": MessageCreate{}, "MESSAGE_UPDATE": MessageUpdate{}, "MESSAGE_DELETE": MessageDelete{}, + "MESSAGE_REACTION_ADD": MessageReactionAdd{}, + "MESSAGE_REACTION_REMOVE": MessageReactionRemove{}, "PRESENCE_UPDATE": PresenceUpdate{}, "PRESENCES_REPLACE": PresencesReplace{}, "READY": Ready{}, @@ -74,6 +76,16 @@ type MessageDelete struct { *Message } +// MessageReactionAdd is a wrapper struct for an event. +type MessageReactionAdd struct { + *MessageReaction +} + +// MessageReactionRemove is a wrapper struct for an event. +type MessageReactionRemove struct { + *MessageReaction +} + // ChannelCreate is a wrapper struct for an event. type ChannelCreate struct { *Channel diff --git a/message.go b/message.go index 7b85973..d7abda6 100644 --- a/message.go +++ b/message.go @@ -28,6 +28,7 @@ type Message struct { Attachments []*MessageAttachment `json:"attachments"` Embeds []*MessageEmbed `json:"embeds"` Mentions []*User `json:"mentions"` + Reactions []*MessageReactions `json:"reactions"` } // A MessageAttachment stores data for message attachments. @@ -110,6 +111,13 @@ type MessageEmbed struct { Fields []*MessageEmbedField `json:"fields,omitempty"` } +// MessageReactions holds a reactions object for a message. +type MessageReactions struct { + Count int `json:"count"` + Me bool `json:"me"` + Emoji *Emoji `json:"emoji"` +} + // ContentWithMentionsReplaced will replace all @ mentions with the // username of the mention. func (m *Message) ContentWithMentionsReplaced() string { diff --git a/restapi.go b/restapi.go index fae8d61..1d5515c 100644 --- a/restapi.go +++ b/restapi.go @@ -1635,3 +1635,52 @@ func (s *Session) WebhookExecute(webhookID, token string, wait bool, data *Webho return } + +// MessageReactionAdd creates an emoji reaction to a message. +// channelID : The channel ID. +// messageID : The message ID. +// emojiID : Either the unicode emoji for the reaction, or a guild emoji identifier. +func (s *Session) MessageReactionAdd(channelID, messageID, emojiID string) error { + + _, err := s.Request("PUT", EndpointMessageReactions(channelID, messageID, emojiID), nil) + + return err +} + +// MessageReactionRemove deletes an emoji reaction to a message. +// channelID : The channel ID. +// messageID : The message ID. +// emojiID : Either the unicode emoji for the reaction, or a guild emoji identifier. +func (s *Session) MessageReactionRemove(channelID, messageID, emojiID string) error { + + _, err := s.Request("DELETE", EndpointMessageReactions(channelID, messageID, emojiID), nil) + + return err +} + +// MessageReactions gets all the users reactions for a specific emoji. +// channelID : The channel ID. +// messageID : The message ID. +// emojiID : Either the unicode emoji for the reaction, or a guild emoji identifier. +// limit : max number of users to return (max 100) +func (s *Session) MessageReactions(channelID, messageID, emojiID string, limit int) (st []*User, err error) { + uri := EndpointMessageReactions(channelID, messageID, emojiID) + + v := url.Values{} + + if limit > 0 { + v.Set("limit", strconv.Itoa(limit)) + } + + if len(v) > 0 { + uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + } + + body, err := s.Request("GET", uri, nil) + if err != nil { + return + } + + err = unmarshal(body, &st) + return +} diff --git a/structs.go b/structs.go index ce4add3..707b2b7 100644 --- a/structs.go +++ b/structs.go @@ -186,6 +186,17 @@ type Emoji struct { RequireColons bool `json:"require_colons"` } +// APIName returns an correctly formatted API name for use in the MessageReactions endpoints. +func (e *Emoji) APIName() string { + if e.ID != "" && e.Name != "" { + return e.Name + ":" + e.ID + } + if e.Name != "" { + return e.Name + } + return e.ID +} + // VerificationLevel type defination type VerificationLevel int @@ -535,6 +546,14 @@ type WebhookParams struct { Embeds []*MessageEmbed `json:"embeds,omitempty"` } +// MessageReaction stores the data for a message reaction. +type MessageReaction struct { + UserID string `json:"user_id"` + MessageID string `json:"message_id"` + Emoji Emoji `json:"emoji"` + ChannelID string `json:"channel_id"` +} + // Constants for the different bit offsets of text channel permissions const ( PermissionReadMessages = 1 << (iota + 10)