Browse Source

Add support for Message Reactions.

Chris Rhodes 8 years ago
parent
commit
c96162c425
5 changed files with 92 additions and 0 deletions
  1. 4 0
      endpoints.go
  2. 12 0
      events.go
  3. 8 0
      message.go
  4. 49 0
      restapi.go
  5. 19 0
      structs.go

+ 4 - 0
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" }

+ 12 - 0
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

+ 8 - 0
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 @<id> mentions with the
 // username of the mention.
 func (m *Message) ContentWithMentionsReplaced() string {

+ 49 - 0
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
+}

+ 19 - 0
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)