Merge pull request #275 from iopred/reactions

Add support for Message Reactions.
This commit is contained in:
Chris Rhodes 2016-11-03 22:14:38 -07:00 committed by GitHub
commit ef4e2224f9
5 changed files with 92 additions and 0 deletions

View file

@ -92,6 +92,10 @@ var (
EndpointWebhook = func(wID string) string { return EndpointWebhooks + wID } EndpointWebhook = func(wID string) string { return EndpointWebhooks + wID }
EndpointWebhookToken = func(wID, token string) string { return EndpointWebhooks + wID + "/" + token } 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 } EndpointInvite = func(iID string) string { return EndpointAPI + "invite/" + iID }
EndpointIntegrationsJoin = func(iID string) string { return EndpointAPI + "integrations/" + iID + "/join" } EndpointIntegrationsJoin = func(iID string) string { return EndpointAPI + "integrations/" + iID + "/join" }

View file

@ -35,6 +35,8 @@ var eventToInterface = map[string]interface{}{
"MESSAGE_CREATE": MessageCreate{}, "MESSAGE_CREATE": MessageCreate{},
"MESSAGE_UPDATE": MessageUpdate{}, "MESSAGE_UPDATE": MessageUpdate{},
"MESSAGE_DELETE": MessageDelete{}, "MESSAGE_DELETE": MessageDelete{},
"MESSAGE_REACTION_ADD": MessageReactionAdd{},
"MESSAGE_REACTION_REMOVE": MessageReactionRemove{},
"PRESENCE_UPDATE": PresenceUpdate{}, "PRESENCE_UPDATE": PresenceUpdate{},
"PRESENCES_REPLACE": PresencesReplace{}, "PRESENCES_REPLACE": PresencesReplace{},
"READY": Ready{}, "READY": Ready{},
@ -74,6 +76,16 @@ type MessageDelete struct {
*Message *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. // ChannelCreate is a wrapper struct for an event.
type ChannelCreate struct { type ChannelCreate struct {
*Channel *Channel

View file

@ -28,6 +28,7 @@ type Message struct {
Attachments []*MessageAttachment `json:"attachments"` Attachments []*MessageAttachment `json:"attachments"`
Embeds []*MessageEmbed `json:"embeds"` Embeds []*MessageEmbed `json:"embeds"`
Mentions []*User `json:"mentions"` Mentions []*User `json:"mentions"`
Reactions []*MessageReactions `json:"reactions"`
} }
// A MessageAttachment stores data for message attachments. // A MessageAttachment stores data for message attachments.
@ -110,6 +111,13 @@ type MessageEmbed struct {
Fields []*MessageEmbedField `json:"fields,omitempty"` 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 // ContentWithMentionsReplaced will replace all @<id> mentions with the
// username of the mention. // username of the mention.
func (m *Message) ContentWithMentionsReplaced() string { func (m *Message) ContentWithMentionsReplaced() string {

View file

@ -1635,3 +1635,52 @@ func (s *Session) WebhookExecute(webhookID, token string, wait bool, data *Webho
return 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
}

View file

@ -186,6 +186,17 @@ type Emoji struct {
RequireColons bool `json:"require_colons"` 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 // VerificationLevel type defination
type VerificationLevel int type VerificationLevel int
@ -535,6 +546,14 @@ type WebhookParams struct {
Embeds []*MessageEmbed `json:"embeds,omitempty"` 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 // Constants for the different bit offsets of text channel permissions
const ( const (
PermissionReadMessages = 1 << (iota + 10) PermissionReadMessages = 1 << (iota + 10)