This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
emoji.go
178 lines (150 loc) · 4.54 KB
/
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package disgord
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/andersfylling/disgord/internal/endpoint"
"github.com/andersfylling/disgord/internal/httd"
)
func validEmojiName(name string) bool {
if name == "" {
return false
}
// TODO: what is the allowed format?
// a test showed that using "-" caused regex issues
return !strings.Contains(name, "-")
}
// Emoji ...
type Emoji struct {
ID Snowflake `json:"id"`
Name string `json:"name"`
Roles []Snowflake `json:"roles,omitempty"`
User *User `json:"user,omitempty"` // the user who created the emoji
RequireColons bool `json:"require_colons,omitempty"`
Managed bool `json:"managed,omitempty"`
Animated bool `json:"animated,omitempty"`
Available bool `json:"available,omitempty"`
}
var _ Reseter = (*Emoji)(nil)
var _ DeepCopier = (*Emoji)(nil)
var _ Copier = (*Emoji)(nil)
var _ Mentioner = (*Emoji)(nil)
// var _ discordSaver = (*Emoji)(nil) // TODO
var _ fmt.Stringer = (*Emoji)(nil)
func (e *Emoji) String() string {
return "emoji{name:" + e.Name + ", id:" + e.ID.String() + "}"
}
// PartialEmoji see Emoji
type PartialEmoji = Emoji
// Mention mentions an emoji. Adds the animation prefix, if animated
func (e *Emoji) Mention() string {
prefix := ":"
if e.Animated {
prefix = "a:"
}
return "<" + prefix + e.Name + ":" + e.ID.String() + ">"
}
//////////////////////////////////////////////////////
//
// REST Methods
//
// https://discord.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.
//
//////////////////////////////////////////////////////
type GuildEmojiQueryBuilder interface {
WithContext(ctx context.Context) GuildEmojiQueryBuilder
WithFlags(flags ...Flag) GuildEmojiQueryBuilder
Get() (*Emoji, error)
Update(params *UpdateEmoji) (*Emoji, error)
Delete() error
}
func (g guildQueryBuilder) Emoji(emojiID Snowflake) GuildEmojiQueryBuilder {
return &guildEmojiQueryBuilder{client: g.client, gid: g.gid, emojiID: emojiID}
}
type guildEmojiQueryBuilder struct {
ctx context.Context
flags Flag
client *Client
gid Snowflake
emojiID Snowflake
}
func (g *guildEmojiQueryBuilder) validate() error {
if g.client == nil {
return ErrMissingClientInstance
}
if g.gid.IsZero() {
return ErrMissingGuildID
}
if g.emojiID.IsZero() {
return ErrMissingEmojiID
}
return nil
}
func (g guildEmojiQueryBuilder) WithContext(ctx context.Context) GuildEmojiQueryBuilder {
g.ctx = ctx
return &g
}
func (g guildEmojiQueryBuilder) WithFlags(flags ...Flag) GuildEmojiQueryBuilder {
g.flags = mergeFlags(flags)
return &g
}
func (g guildEmojiQueryBuilder) Get() (*Emoji, error) {
if !ignoreCache(g.flags) {
if emoji, _ := g.client.cache.GetGuildEmoji(g.gid, g.emojiID); emoji != nil {
return emoji, nil
}
}
r := g.client.newRESTRequest(&httd.Request{
Endpoint: endpoint.GuildEmoji(g.gid, g.emojiID),
Ctx: g.ctx,
}, g.flags)
r.pool = g.client.pool.emoji
r.factory = func() interface{} {
return &Emoji{}
}
return getEmoji(r.Execute)
}
// Update Modify the given emoji. Requires the 'MANAGE_EMOJIS' permission.
// Returns the updated emoji object on success. Fires a Guild Emojis Update Gateway event.
func (g guildEmojiQueryBuilder) Update(params *UpdateEmoji) (*Emoji, error) {
if params == nil {
return nil, ErrMissingRESTParams
}
if err := g.validate(); err != nil {
return nil, err
}
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodPatch,
Ctx: g.ctx,
Endpoint: endpoint.GuildEmoji(g.gid, g.emojiID),
ContentType: httd.ContentTypeJSON,
Body: params,
Reason: params.AuditLogReason,
}, g.flags)
r.pool = g.client.pool.emoji
r.factory = func() interface{} {
return &Emoji{}
}
return getEmoji(r.Execute)
}
type UpdateEmoji struct {
Name *string `json:"name,omitempty"`
Roles *[]Snowflake `json:"roles,omitempty"`
AuditLogReason string `json:"-"`
}
// Delete deletes the given emoji. Requires the 'MANAGE_EMOJIS' permission. Returns 204 No Content on
// success. Fires a Guild Emojis Update Gateway event.
func (g guildEmojiQueryBuilder) Delete() (err error) {
r := g.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Endpoint: endpoint.GuildEmoji(g.gid, g.emojiID),
Ctx: g.ctx,
}, g.flags)
_, err = r.Execute()
return
}