forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_emoji.go
157 lines (141 loc) · 5.78 KB
/
rest_emoji.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package disgord
import (
"errors"
"net/http"
"strings"
"github.com/andersfylling/disgord/endpoint"
"github.com/andersfylling/disgord/httd"
)
// endpoints
//
// https://discordapp.com/developers/docs/resources/emoji#emoji-resource
// Routes for controlling emojis do not follow the normal rate limit conventions.
// These routes are specifically limited on a per-guild basis to prevent abuse.
// This means that the quota returned by our APIs may be inaccurate,
// and you may encounter 429s.
// ListGuildEmojis [GET] Returns a list of emoji objects for the given guild.
// Endpoint /guilds/{guild.id}/emojis
// Rate limiter [MAJOR] /guilds/{guild.id} // TODO: no idea if this is correct
// Discord documentation https://discordapp.com/developers/docs/resources/emoji#list-guild-emojis
// Reviewed 2018-06-10
// Comment -
func ListGuildEmojis(client httd.Getter, id Snowflake) (ret []*Emoji, err error) {
_, body, err := client.Get(&httd.Request{
Ratelimiter: ratelimitGuild(id),
Endpoint: endpoint.GuildEmojis(id),
})
if err != nil {
return
}
err = unmarshal(body, &ret)
return
}
// GetGuildEmoji [GET] Returns an emoji object for the given guild and emoji IDs.
// Endpoint /guilds/{guild.id}/emojis/{emoji.id}
// Rate limiter [MAJOR] /guilds/{guild.id} // TODO: no idea if this is correct
// Discord documentation https://discordapp.com/developers/docs/resources/emoji#get-guild-emoji
// Reviewed 2018-06-10
// Comment -
func GetGuildEmoji(client httd.Getter, guildID, emojiID Snowflake) (ret *Emoji, err error) {
_, body, err := client.Get(&httd.Request{
Ratelimiter: ratelimitGuild(guildID),
Endpoint: endpoint.GuildEmoji(guildID, emojiID),
})
if err != nil {
return
}
err = unmarshal(body, &ret)
return
}
func validEmojiName(name string) bool {
// TODO: what is the allowed format?
// a test showed that using "-" caused regex issues
return !strings.Contains(name, "-")
}
// CreateGuildEmojiParams JSON params for func CreateGuildEmoji
type CreateGuildEmojiParams struct {
Name string `json:"name"`
Image string `json:"image"`
Roles []Snowflake `json:"roles"`
}
// CreateGuildEmoji [POST] Create a new emoji for the guild. Requires the
// 'MANAGE_EMOJIS' permission. Returns the new emoji
// object on success. Fires a Guild Emojis Update Gateway event.
// Endpoint /guilds/{guild.id}/emojis
// Rate limiter [MAJOR] /guilds/{guild.id} // TODO: no idea if this is correct
// Discord documentation https://discordapp.com/developers/docs/resources/emoji#create-guild-emoji
// Reviewed 2018-06-10
// Comment Emojis and animated emojis have a maximum file size of 256kb.
// Attempting to upload an emoji larger than this limit will fail
// and return 400 Bad Request and an error message, but not a JSON
// status code.
func CreateGuildEmoji(client httd.Poster, guildID Snowflake, params *CreateGuildEmojiParams) (ret *Emoji, err error) {
if !validEmojiName(params.Name) {
err = errors.New("emoji name contains illegal characters. Did not send request")
return
}
_, body, err := client.Post(&httd.Request{
Ratelimiter: ratelimitGuild(guildID),
Endpoint: endpoint.GuildEmojis(guildID),
JSONParams: params,
ContentType: httd.ContentTypeJSON,
})
if err != nil {
return
}
err = unmarshal(body, &ret)
return
}
// ModifyGuildEmojiParams JSON params for func ModifyGuildEmoji
type ModifyGuildEmojiParams struct {
Name string `json:"name"`
Roles []Snowflake `json:"roles"`
}
// ModifyGuildEmoji [PATCH] Modify the given emoji. Requires the 'MANAGE_EMOJIS'
// permission. Returns the updated emoji object on success.
// Fires a Guild Emojis Update Gateway event.
// Endpoint /guilds/{guild.id}/emojis/{emoji.id}
// Rate limiter [MAJOR] /guilds/{guild.id} // TODO: no idea if this is correct
// Discord documentation https://discordapp.com/developers/docs/resources/emoji#modify-guild-emoji
// Reviewed 2018-06-10
// Comment -
func ModifyGuildEmoji(client httd.Patcher, guildID, emojiID Snowflake, params *ModifyGuildEmojiParams) (ret *Emoji, err error) {
if !validEmojiName(params.Name) {
err = errors.New("emoji name contains illegal characters. Did not send request")
return
}
_, body, err := client.Patch(&httd.Request{
Ratelimiter: ratelimitGuild(guildID),
Endpoint: endpoint.GuildEmoji(guildID, emojiID),
JSONParams: params,
ContentType: httd.ContentTypeJSON,
})
if err != nil {
return
}
err = unmarshal(body, &ret)
return
}
// DeleteGuildEmoji [DELETE] Delete the given emoji. Requires the
// 'MANAGE_EMOJIS' permission. Returns 204
// No Content on success. Fires a Guild Emojis
// Update Gateway event.
// Endpoint /guilds/{guild.id}/emojis/{emoji.id}
// Rate limiter [MAJOR] /guilds/{guild.id} // TODO: no idea if this is correct
// Discord documentation https://discordapp.com/developers/docs/resources/emoji#delete-guild-emoji
// Reviewed 2018-06-10
// Comment -
func DeleteGuildEmoji(client httd.Deleter, guildID, emojiID Snowflake) (err error) {
resp, _, err := client.Delete(&httd.Request{
Ratelimiter: ratelimitGuild(guildID),
Endpoint: endpoint.GuildEmoji(guildID, emojiID),
})
if err != nil {
return
}
if resp.StatusCode != http.StatusNoContent {
msg := "unexpected http response code. Got " + resp.Status + ", wants " + http.StatusText(http.StatusNoContent)
err = errors.New(msg)
}
return
}