1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-01 03:03:48 +00:00
arikawa/api/emoji.go

95 lines
2.4 KiB
Go
Raw Normal View History

2020-01-02 05:39:52 +00:00
package api
import (
2020-01-04 04:19:24 +00:00
"errors"
"github.com/diamondburned/arikawa/discord"
2020-01-15 18:32:54 +00:00
"github.com/diamondburned/arikawa/internal/httputil"
2020-01-02 05:39:52 +00:00
)
// EmojiAPI is a special format that the API wants.
type EmojiAPI = string
func FormatEmojiAPI(id discord.Snowflake, name string) string {
if id == 0 {
return name
}
return id.String() + ":" + name
}
func (c *Client) Emojis(
guildID discord.Snowflake) ([]discord.Emoji, error) {
2020-01-02 05:39:52 +00:00
var emjs []discord.Emoji
return emjs, c.RequestJSON(&emjs, "GET",
EndpointGuilds+guildID.String()+"/emojis")
2020-01-02 05:39:52 +00:00
}
func (c *Client) Emoji(
guildID, emojiID discord.Snowflake) (*discord.Emoji, error) {
2020-01-02 05:39:52 +00:00
var emj *discord.Emoji
return emj, c.RequestJSON(&emj, "GET",
EndpointGuilds+guildID.String()+"/emojis/"+emojiID.String())
2020-01-02 05:39:52 +00:00
}
2020-01-04 04:19:24 +00:00
var (
ErrEmojiTooLarge = errors.New("Emoji is larger than 256k")
)
// CreateEmoji creates a new emoji in the guild. This endpoint requires
// MANAGE_EMOJIS. ContentType must be "image/jpeg", "image/png", or
// "image/gif". However, ContentType can also be automatically detected
// (though shouldn't be relied on). Roles slice is optional.
func (c *Client) CreateEmoji(
guildID discord.Snowflake, name string, image Image,
roles []discord.Snowflake) (*discord.Emoji, error) {
image.MaxSize = 256 * 1000
if err := image.Validate(); err != nil {
return nil, err
}
var param struct {
Name string `json:"name"`
Image Image `json:"image"`
Roles []discord.Snowflake `json:"roles"`
}
param.Name = name
param.Roles = roles
param.Image = image
var emj *discord.Emoji
return emj, c.RequestJSON(
&emj, "POST",
EndpointGuilds+guildID.String()+"/emojis",
httputil.WithJSONBody(c, param),
)
}
// ModifyEmoji changes an existing emoji. This requires MANAGE_EMOJIS. Name and
// roles are optional fields (though you'd want to change either though).
func (c *Client) ModifyEmoji(
guildID, emojiID discord.Snowflake, name string,
roles []discord.Snowflake) error {
var param struct {
Name string `json:"name,omitempty"`
Roles []discord.Snowflake `json:"roles,omitempty"`
}
return c.FastRequest(
"PATCH",
EndpointGuilds+guildID.String()+"/emojis/"+emojiID.String(),
httputil.WithJSONBody(c, param),
)
}
// DeleteEmoji requires MANAGE_EMOJIS.
func (c *Client) DeleteEmoji(guildID, emojiID discord.Snowflake) error {
return c.FastRequest("DELETE",
EndpointGuilds+guildID.String()+"/emojis/"+emojiID.String())
}