1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-17 20:32:48 +00:00
arikawa/api/message_reaction.go

158 lines
4.3 KiB
Go
Raw Normal View History

2020-01-02 05:39:52 +00:00
package api
import (
2020-05-11 19:03:33 +00:00
"net/url"
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/utils/httputil"
2020-01-02 05:39:52 +00:00
)
2020-05-11 22:06:19 +00:00
// React creates a reaction for the message.
//
// This endpoint requires the READ_MESSAGE_HISTORY permission to be present on
// the current user. Additionally, if nobody else has reacted to the message
// using this emoji, this endpoint requires the 'ADD_REACTIONS' permission to
// be present on the current user.
2020-05-12 02:05:08 +00:00
func (c *Client) React(channelID, messageID discord.Snowflake, emoji Emoji) error {
var msgURL = EndpointChannels + channelID.String() +
"/messages/" + messageID.String() +
2020-05-11 19:03:33 +00:00
"/reactions/" + url.PathEscape(emoji) + "/@me"
2020-01-02 05:39:52 +00:00
return c.FastRequest("PUT", msgURL)
}
2020-05-11 22:06:19 +00:00
// Unreact removes a reaction the current user has made for the message.
2020-05-12 02:05:08 +00:00
func (c *Client) Unreact(chID, msgID discord.Snowflake, emoji Emoji) error {
2020-01-23 05:13:53 +00:00
return c.DeleteUserReaction(chID, msgID, 0, emoji)
}
2020-05-11 22:06:19 +00:00
// Reactions returns reactions up to the specified limit. It will paginate
// automatically.
//
// Max can be 0, in which case the function will try and fetch all reactions.
func (c *Client) Reactions(
2020-05-12 02:05:08 +00:00
channelID, messageID discord.Snowflake, max uint, emoji Emoji) ([]discord.User, error) {
2020-01-02 05:39:52 +00:00
2020-01-06 05:22:26 +00:00
var users []discord.User
var after discord.Snowflake = 0
const hardLimit int = 100
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
2020-01-18 19:34:08 +00:00
if max > 0 {
if fetch > max {
fetch = max
}
max -= fetch
}
r, err := c.ReactionsRange(channelID, messageID, 0, after, fetch, emoji)
2020-01-06 05:22:26 +00:00
if err != nil {
return users, err
}
users = append(users, r...)
if len(r) < hardLimit {
2020-01-06 05:22:26 +00:00
break
}
after = r[hardLimit-1].ID
2020-01-06 05:22:26 +00:00
}
return users, nil
}
2020-05-11 22:06:19 +00:00
// ReactionsBefore gets all reactions before the passed user ID.
func (c *Client) ReactionsBefore(
channelID, messageID, before discord.Snowflake,
2020-05-11 19:03:33 +00:00
limit uint, emoji Emoji) ([]discord.User, error) {
2020-01-06 05:22:26 +00:00
return c.ReactionsRange(channelID, messageID, before, 0, limit, emoji)
2020-01-06 05:22:26 +00:00
}
// Refer to ReactionsRange.
func (c *Client) ReactionsAfter(
channelID, messageID, after discord.Snowflake,
2020-05-11 19:03:33 +00:00
limit uint, emoji Emoji) ([]discord.User, error) {
2020-01-06 05:22:26 +00:00
return c.ReactionsRange(channelID, messageID, 0, after, limit, emoji)
2020-01-02 05:39:52 +00:00
}
2020-01-06 05:22:26 +00:00
// ReactionsRange get users before and after IDs. Before, after, and limit are
// optional. A maximum limit of only 100 reactions could be returned.
func (c *Client) ReactionsRange(
channelID, messageID, before, after discord.Snowflake,
2020-05-11 19:03:33 +00:00
limit uint, emoji Emoji) ([]discord.User, error) {
2020-01-02 05:39:52 +00:00
2020-05-11 22:06:19 +00:00
switch {
case limit == 0:
2020-01-02 05:39:52 +00:00
limit = 25
2020-05-11 22:06:19 +00:00
case limit > 100:
2020-01-02 05:39:52 +00:00
limit = 100
}
2020-01-06 03:48:39 +00:00
var param struct {
Before discord.Snowflake `schema:"before,omitempty"`
After discord.Snowflake `schema:"after,omitempty"`
2020-01-02 05:39:52 +00:00
2020-01-06 03:48:39 +00:00
Limit uint `schema:"limit"`
2020-01-02 05:39:52 +00:00
}
2020-01-06 03:48:39 +00:00
param.Before = before
param.After = after
param.Limit = limit
var users []discord.User
2020-01-06 05:22:26 +00:00
return users, c.RequestJSON(
&users, "GET", EndpointChannels+channelID.String()+
"/messages/"+messageID.String()+
2020-05-11 19:03:33 +00:00
"/reactions/"+url.PathEscape(emoji),
2020-01-06 05:22:26 +00:00
httputil.WithSchema(c, param),
)
2020-01-02 05:39:52 +00:00
}
2020-05-11 22:06:19 +00:00
// DeleteReaction deletes another user's reaction.
//
// This endpoint requires the MANAGE_MESSAGES permission to be present on the
// current user.
2020-01-23 05:13:53 +00:00
func (c *Client) DeleteUserReaction(
2020-05-12 02:05:08 +00:00
channelID, messageID, userID discord.Snowflake, emoji Emoji) error {
2020-01-02 05:39:52 +00:00
var user = "@me"
if userID > 0 {
user = userID.String()
}
2020-05-12 02:05:08 +00:00
return c.FastRequest(
"DELETE",
EndpointChannels+channelID.String()+"/messages/"+messageID.String()+
"/reactions/"+url.PathEscape(emoji)+"/"+user,
)
2020-01-02 05:39:52 +00:00
}
2020-05-11 22:06:19 +00:00
// DeleteReactions deletes all the reactions for a given emoji on a message.
//
// This endpoint requires the MANAGE_MESSAGES permission to be present on the
// current user.
// Fires a Message Reaction Remove Emoji Gateway event.
2020-01-23 05:13:53 +00:00
func (c *Client) DeleteReactions(
2020-05-12 02:05:08 +00:00
channelId, messageID discord.Snowflake, emoji Emoji) error {
2020-01-02 05:39:52 +00:00
return c.FastRequest(
2020-05-12 02:05:08 +00:00
"DELETE",
EndpointChannels+channelId.String()+"/messages/"+messageID.String()+
"/reactions/"+url.PathEscape(emoji),
)
2020-01-02 05:39:52 +00:00
}
2020-05-11 22:06:19 +00:00
// DeleteAllReactions deletes all reactions on a message.
//
// This endpoint requires the MANAGE_MESSAGES permission to be present on the
// current user.
// Fires a Message Reaction Remove All Gateway event.
func (c *Client) DeleteAllReactions(channelID, messageID discord.Snowflake) error {
2020-05-12 02:05:08 +00:00
return c.FastRequest(
"DELETE",
EndpointChannels+channelID.String()+"/messages/"+messageID.String()+"/reactions/",
)
2020-01-02 05:39:52 +00:00
}