1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-05 06:26:08 +00:00
arikawa/api/message_reaction.go

133 lines
3.2 KiB
Go
Raw Normal View History

2020-01-02 05:39:52 +00:00
package api
import (
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/httputil"
2020-01-02 05:39:52 +00:00
)
// React adds a reaction to the message. This requires READ_MESSAGE_HISTORY (and
// additionally ADD_REACTIONS) to react.
func (c *Client) React(
channelID, messageID discord.Snowflake, emoji EmojiAPI) error {
2020-01-02 05:39:52 +00:00
var msgURL = EndpointChannels + channelID.String() +
"/messages/" + messageID.String() +
2020-01-02 05:39:52 +00:00
"/reactions/" + emoji + "/@me"
return c.FastRequest("PUT", msgURL)
}
2020-01-06 05:22:26 +00:00
// Reactions returns all reactions. It will paginate automatically.
func (c *Client) Reactions(
channelID, messageID discord.Snowflake,
max uint, emoji EmojiAPI) ([]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) {
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
}
// Refer to ReactionsRange.
func (c *Client) ReactionsBefore(
channelID, messageID, before discord.Snowflake,
2020-01-06 05:22:26 +00:00
limit uint, emoji EmojiAPI) ([]discord.User, error) {
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-01-06 05:22:26 +00:00
limit uint, emoji EmojiAPI) ([]discord.User, error) {
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,
limit uint, emoji EmojiAPI) ([]discord.User, error) {
2020-01-02 05:39:52 +00:00
if limit == 0 {
limit = 25
}
if limit > 100 {
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-01-06 05:22:26 +00:00
"/reactions/"+emoji,
httputil.WithSchema(c, param),
)
2020-01-02 05:39:52 +00:00
}
// DeleteReaction requires MANAGE_MESSAGES if not @me.
func (c *Client) DeleteReaction(
chID, msgID, userID discord.Snowflake, emoji EmojiAPI) error {
var user = "@me"
if userID > 0 {
user = userID.String()
}
var msgURL = EndpointChannels + chID.String() +
"/messages/" + msgID.String() +
"/reactions/" + emoji + "/" + user
return c.FastRequest("DELETE", msgURL)
}
func (c *Client) DeleteOwnReaction(
chID, msgID discord.Snowflake, emoji EmojiAPI) error {
return c.DeleteReaction(chID, msgID, 0, emoji)
}
// DeleteAllReactions equires MANAGE_MESSAGE.
func (c *Client) DeleteAllReactions(
chID, msgID discord.Snowflake, emoji EmojiAPI) error {
var msgURL = EndpointChannels + chID.String() +
"/messages/" + msgID.String() +
"/reactions/" + emoji
return c.FastRequest("DELETE", msgURL)
}