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
/
reaction.go
249 lines (215 loc) · 7.83 KB
/
reaction.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package disgord
import (
"context"
"errors"
"net/http"
"github.com/andersfylling/disgord/internal/endpoint"
"github.com/andersfylling/disgord/internal/httd"
)
// Reaction ...
// https://discord.com/developers/docs/resources/channel#reaction-object
type Reaction struct {
Count uint `json:"count"`
Me bool `json:"me"`
Emoji *PartialEmoji `json:"Emoji"`
}
var _ Reseter = (*Reaction)(nil)
var _ Copier = (*Reaction)(nil)
var _ DeepCopier = (*Reaction)(nil)
func emojiReference(i interface{}) (string, error) {
var emojiCode string
if e, ok := i.(*Emoji); ok {
if e.ID.IsZero() {
emojiCode = e.Name
} else {
emojiCode = e.Name + ":" + e.ID.String()
}
} else if _, ok := i.(string); ok {
emojiCode = i.(string) // unicode
emojiCode = unwrapEmoji(emojiCode)
} else {
return "", errors.New("emoji type can only be a unicode string or a *Emoji struct")
}
return emojiCode, nil
}
func unwrapEmoji(e string) string {
l := len(e)
if l >= 2 && e[0] == e[l-1] && e[0] == ':' {
// :emoji: => emoji
e = e[1 : l-1]
}
return e
}
type ReactionQueryBuilder interface {
WithContext(ctx context.Context) ReactionQueryBuilder
WithFlags(flags ...Flag) ReactionQueryBuilder
// Create create 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. Returns a 204
// empty response on success. The maximum request size when sending a message is 8MB.
Create() (err error)
// Get Get a list of Users that reacted with this emoji. Returns an array of user objects on success.
Get(params URLQueryStringer) (reactors []*User, err error)
// DeleteOwn Delete a reaction the current user has made for the message.
// Returns a 204 empty response on success.
DeleteOwn() (err error)
// DeleteUser Deletes another user's reaction. This endpoint requires the 'MANAGE_MESSAGES' permission
// to be present on the current user. Returns a 204 empty response on success.
DeleteUser(userID Snowflake) (err error)
}
func (m messageQueryBuilder) Reaction(emoji interface{}) ReactionQueryBuilder {
return &reactionQueryBuilder{client: m.client, cid: m.cid, mid: m.mid, emoji: emoji}
}
type reactionQueryBuilder struct {
ctx context.Context
flags Flag
client *Client
cid Snowflake
mid Snowflake
emoji interface{}
}
func (r reactionQueryBuilder) WithContext(ctx context.Context) ReactionQueryBuilder {
r.ctx = ctx
return &r
}
func (r reactionQueryBuilder) WithFlags(flags ...Flag) ReactionQueryBuilder {
r.flags = mergeFlags(flags)
return &r
}
// Create [REST] Create 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. Returns a 204 empty
// response on success. The maximum request size when sending a message is 8MB.
//
// Method PUT
// Endpoint /channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
// Discord documentation https://discord.com/developers/docs/resources/channel#create-reaction
// Reviewed 2019-01-30
// Comment emoji either unicode (string) or *Emoji with an snowflake Snowflake if it's custom
func (r reactionQueryBuilder) Create() error {
if r.cid.IsZero() {
return ErrMissingChannelID
}
if r.mid.IsZero() {
return ErrMissingMessageID
}
if r.emoji == nil {
return errors.New("emoji must be set in order to create a message reaction")
}
emojiCode, err := emojiReference(r.emoji)
if err != nil {
return err
}
req := r.client.newRESTRequest(&httd.Request{
Method: http.MethodPut,
Endpoint: endpoint.ChannelMessageReactionMe(r.cid, r.mid, emojiCode),
Ctx: r.ctx,
}, r.flags)
_, err = req.Execute()
return err
}
// DeleteOwn [REST] Delete a reaction the current user has made for the message.
// Returns a 204 empty response on success.
//
// Method DELETE
// Endpoint /channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
// Discord documentation https://discord.com/developers/docs/resources/channel#delete-own-reaction
// Reviewed 2019-01-28
// Comment emoji either unicode (string) or *Emoji with an snowflake Snowflake if it's custom
func (r reactionQueryBuilder) DeleteOwn() error {
if r.cid.IsZero() {
return ErrMissingChannelID
}
if r.mid.IsZero() {
return ErrMissingMessageID
}
if r.emoji == nil {
return errors.New("emoji must be set in order to create a message reaction")
}
emojiCode, err := emojiReference(r.emoji)
if err != nil {
return err
}
req := r.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Endpoint: endpoint.ChannelMessageReactionMe(r.cid, r.mid, emojiCode),
Ctx: r.ctx,
}, r.flags)
_, err = req.Execute()
return err
}
// DeleteUser [REST] Deletes another user's reaction. This endpoint requires the 'MANAGE_MESSAGES' permission
// to be present on the current user. Returns a 204 empty response on success.
//
// Method DELETE
// Endpoint /channels/{channel.id}/messages/{message.id}/reactions/{emoji}/@me
// Discord documentation https://discord.com/developers/docs/resources/channel#delete-user-reaction
// Reviewed 2019-01-28
// Comment emoji either unicode (string) or *Emoji with an snowflake Snowflake if it's custom
func (r reactionQueryBuilder) DeleteUser(userID Snowflake) error {
if r.cid.IsZero() {
return ErrMissingChannelID
}
if r.mid.IsZero() {
return ErrMissingMessageID
}
if r.emoji == nil {
return errors.New("emoji must be set in order to create a message reaction")
}
if userID.IsZero() {
return ErrMissingUserID
}
emojiCode, err := emojiReference(r.emoji)
if err != nil {
return err
}
req := r.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Endpoint: endpoint.ChannelMessageReactionUser(r.cid, r.mid, emojiCode, userID),
Ctx: r.ctx,
}, r.flags)
_, err = req.Execute()
return err
}
// GetReactionURL https://discord.com/developers/docs/resources/channel#get-reactions-query-string-params
type GetReactionURL struct {
Before Snowflake `urlparam:"before,omitempty"` // get Users before this user Snowflake
After Snowflake `urlparam:"after,omitempty"` // get Users after this user Snowflake
Limit int `urlparam:"limit,omitempty"` // max number of Users to return (1-100)
}
var _ URLQueryStringer = (*GetReactionURL)(nil)
// Get [REST] Get a list of Users that reacted with this emoji. Returns an array of user objects on success.
//
// Method GET
// Endpoint /channels/{channel.id}/messages/{message.id}/reactions/{emoji}
// Discord documentation https://discord.com/developers/docs/resources/channel#get-reactions
// Reviewed 2019-01-28
// Comment emoji either unicode (string) or *Emoji with an snowflake Snowflake if it's custom
func (r reactionQueryBuilder) Get(params URLQueryStringer) (ret []*User, err error) {
if r.cid.IsZero() {
return nil, ErrMissingChannelID
}
if r.mid.IsZero() {
return nil, ErrMissingMessageID
}
if r.emoji == nil {
return nil, errors.New("emoji must be set in order to create a message reaction")
}
emojiCode, err := emojiReference(r.emoji)
if err != nil {
return nil, err
}
query := ""
if params != nil {
query += params.URLQueryString()
}
req := r.client.newRESTRequest(&httd.Request{
Endpoint: endpoint.ChannelMessageReaction(r.cid, r.mid, emojiCode) + query,
Ctx: r.ctx,
}, r.flags)
req.factory = func() interface{} {
tmp := make([]*User, 0)
return &tmp
}
return getUsers(req.Execute)
}