-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_client.go
306 lines (252 loc) · 10.2 KB
/
rest_client.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package godiscord
import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/url"
)
const (
restBaseURL = "https://discord.com"
apiVersion = 10
)
type transport struct {
authToken string
underlyingTransport http.RoundTripper
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", fmt.Sprintf("Bot %s", t.authToken))
return t.underlyingTransport.RoundTrip(req)
}
type RestClient interface{}
func newRestClient(client *http.Client, authToken string) (*restClient, error) {
baseURL, err := url.JoinPath(restBaseURL, fmt.Sprintf("api/v%d", apiVersion))
if err != nil {
return nil, fmt.Errorf("failed to build base url: %w", err)
}
restClient := &restClient{
httpClient: client,
baseURL: baseURL,
authToken: authToken,
}
restClient.httpClient.Transport = &transport{
underlyingTransport: http.DefaultTransport,
authToken: authToken,
}
return restClient, nil
}
type restClient struct {
httpClient *http.Client
baseURL string
authToken string
}
type apiError struct {
Message string `json:"message,omitempty"`
Code int `json:"code,omitempty"`
}
func (e *apiError) Error() string {
return fmt.Sprintf("discord api error: %d: %s", e.Code, e.Message)
}
func (c *restClient) get(path string, resp any) error {
return c.do(http.MethodGet, path, nil, resp)
}
func (c *restClient) delete(path string, resp any) error {
return c.do(http.MethodDelete, path, nil, resp)
}
func (c *restClient) patch(path string, req, resp any) error {
return c.do(http.MethodPatch, path, req, resp)
}
func (c *restClient) put(path string, req, resp any) error {
return c.do(http.MethodPut, path, req, resp)
}
func (c *restClient) post(path string, req, resp any) error {
return c.do(http.MethodPost, path, req, resp)
}
// do is a helper function for doing a request.
func (c *restClient) do(method string, path string, reqStruct any, respStruct any) error {
u, err := url.JoinPath(c.baseURL, path)
if err != nil {
return fmt.Errorf("failed to build url: %w", err)
}
slog.Info("Making request.", "method", method, "path", path)
var body *bytes.Reader
if reqStruct != nil {
bs, err := json.Marshal(reqStruct)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
body = bytes.NewReader(bs)
}
var req *http.Request
if body != nil {
req, err = http.NewRequest(method, u, body)
if req != nil {
req.Header.Add("Content-Type", "application/json")
}
} else {
req, err = http.NewRequest(method, u, nil)
}
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("User-Agent", "DiscordBot (https://github.com/hagesjo/godiscord, dev)")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to do request: %w", err)
}
if resp.StatusCode/100 != 2 {
var apiErr apiError
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return fmt.Errorf("failed to decode error response, status code %d: %w", resp.StatusCode, err)
}
return &apiErr
}
if respStruct != nil {
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(respStruct); err != nil {
return fmt.Errorf("failed to decode json response: %w", err)
}
}
return nil
}
type GetGatewayURLResp struct {
URL string `json:"url"`
Shards int `json:"shards"`
SessionStartLimit struct {
Total int `json:"total"`
Remaining int `json:"remaining"`
ResetAfter int `json:"reset_after"`
MaxConcurrency int `json:"max_concurrency"`
} `json:"session_start_limit"`
}
func (c *restClient) GetGatewayURL() (string, error) {
var resp GetGatewayURLResp
err := c.get("/gateway/bot", &resp)
if err != nil {
return "", err
}
return fmt.Sprintf("%s?v=%d&encoding=json", resp.URL, apiVersion), nil
}
// MessageCreateRequest is the request used for creating a message.
// At least one of content, embeds, sticker_ids, components, or files[n] is required.
// An example:
//
// {
// "content": "Hello, World!",
// "tts": false,
// "embeds": [{
// "title": "Hello, Embed!",
// "description": "This is an embedded message."
// }]
// }
type MessageCreateRequest struct {
Content string `json:"content,omitempty"` // Message contents (up to 2000 characters).
Nonce string `json:"nonce,omitempty"` // Can be used to verify a message was sent (up to 25 characters). Value will appear in the Message Create event.
TTS bool `json:"tts,omitempty"` // true if this is a TTS message
Embeds []Embed `json:"embeds,omitempty"` // Up to 10 rich embeds (up to 6000 characters).
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"` // Allowed mentions for the message.
MessageReference *MessageReference `json:"message_reference,omitempty"` // Include to make your message a reply.
Components []MessageActionType `json:"components,omitempty"` // Components to include with the message.
StickerIDs []string `json:"sticker_ids,omitempty"` // IDs of up to 3 stickers in the server to send in the message.
Files map[string][]byte `json:"files,omitempty"` // Contents of the file being sent. See Uploading Files.
PayloadJSON string `json:"payload_json,omitempty"` // JSON-encoded body of non-file params, only for multipart/form-data requests. See Uploading Files.
Attachments []MessageAttachment `json:"attachments,omitempty"` // Attachment objects with filename and description. See Uploading Files.
Flags int `json:"flags,omitempty"` // Message flags combined as a bitfield (only SUPPRESS_EMBEDS and SUPPRESS_NOTIFICATIONS can be set).
}
type MessageCreateResponse struct {
// There's more fields here, but only need ID for now.
ID string `json:"id,omitempty"`
}
func (c *restClient) GetGuild(guildID string) error {
path := fmt.Sprintf("/guilds/%s", guildID)
var resp Guild
if err := c.get(path, &resp); err != nil {
return err
}
return nil
}
// TODO: ModifyGuild
// TODO: GetGuildPreview
// TODO: DeleteGuild
// TODO: loooooads more.
func (c *restClient) MessageSend(channelID string, req MessageCreateRequest) (*MessageCreateResponse, error) {
path := fmt.Sprintf("/channels/%s/messages", channelID)
resp := &MessageCreateResponse{}
err := c.post(path, req, resp)
if err != nil {
return nil, err
}
return resp, nil
}
type CreateThreadRequest struct {
// Name is the channel name and must be between 1 and 100 characters.
Name string `json:"name" validate:"required,min=1,max=100"`
// AutoArchiveDuration is the number of minutes before a thread stops showing in the channel list due to inactivity.
// It can be set to one of the following values: 60, 1440, 4320, 10080. This field is optional.
AutoArchiveDuration *int `json:"auto_archive_duration,omitempty" validate:"omitempty,oneof=60 1440 4320 10080"`
// RateLimitPerUser is the number of seconds a user has to wait before sending another message.
// It must be between 0 and 21600 seconds. This field is optional.
RateLimitPerUser *int `json:"rate_limit_per_user,omitempty" validate:"omitempty,min=0,max=21600"`
}
type CreateThreadResponse struct {
// There's more fields here, but only need ID for now.
ID string `json:"id,omitempty"`
}
func (c *restClient) CreateThread(channelID, messageID string, req CreateThreadRequest) (*CreateThreadResponse, error) {
path := fmt.Sprintf("/channels/%s/messages/%s/threads", channelID, messageID)
resp := &CreateThreadResponse{}
err := c.post(path, req, resp)
if err != nil {
return nil, err
}
return resp, nil
}
type CreateChannelRequest struct {
Name *string `json:"name,omitempty"`
Type ChannelType `json:"type"`
Topic *string `json:"topic,omitempty"`
Bitrate *int `json:"bitrate,omitempty"`
UserLimit *int `json:"user_limit,omitempty"`
// RateLimitPerUser also applies to thread creation. Users can send one message and create one thread during each rate_limit_per_user interval.
RateLimitPerUser *int `json:"rate_limit_per_user,omitempty"`
Position *int `json:"position,omitempty"`
PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites,omitempty"`
// ParentID is the parent id of the channel.
// For guild channels: id of the parent category for a channel (each parent category can contain up to 50 channels).
// For threads: id of the text channel this thread was created.
ParentID *string `json:"parent_id,omitempty"`
NSFW bool `json:"nsfw"`
RTCRegion *string `json:"rtc_region,omitempty"`
VideoQualityMode *VideoQualityMode `json:"video_quality_mode,omitempty"`
DefaultAutoArchiveDuration *int `json:"default_auto_archive_duration,omitempty"`
DefaultReactionEmoji *DefaultReaction `json:"default_reaction_emoji,omitempty"`
AvailableTags []ForumTag `json:"available_tags,omitempty"`
DefaultSortOrder *ChannelSortOrder `json:"default_sort_order,omitempty"`
DefaultForumLayout *ChannelForumLayout `json:"default_forum_layout,omitempty"`
DefaultThreadRateLimitPerUser *int `json:"default_thread_rate_limit_per_user,omitempty"`
}
func (c *restClient) CreateChannel(guildID string, req CreateChannelRequest) error {
path := fmt.Sprintf("/guilds/%s/channels", guildID)
err := c.post(path, req, nil)
if err != nil {
return err
}
return nil
}
type ModifyChannelOrder struct {
ID string `json:"id"` // snowflake
ChannelID string `json:"channel_id"` // snowflake
Position *int `json:"position"` // ?integer
LockPermissions *bool `json:"lock_permissions"` // ?boolean
ParentID *string `json:"parent_id"` // ?snowflake
}
type ModifyChannelOrderRequest []ModifyChannelOrder
func (c *restClient) ModifyChannelOrder(channelID string, req CreateChannelRequest) error {
path := fmt.Sprintf("/guilds/%s/channels", channelID)
err := c.post(path, req, nil)
if err != nil {
return err
}
return nil
}