-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
184 lines (150 loc) · 4.85 KB
/
request.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
package httpcord
import (
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"github.com/valyala/fasthttp"
"golang.org/x/exp/maps"
"httpcord/endpoints"
)
const (
ReasonHeaderKey = "X-Audit-Log-Reason"
AuthorizationHeaderKey = "Authorization"
UserAgentHeaderKey = "User-Agent"
)
var DefaultUserAgent = fmt.Sprintf("HttpInteractionsBot (http-cord, %s)", VERSION)
// Request Create a request
func Request(URI, method string, body interface{}, clientToken string, headers map[string]string) []byte {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(URI)
if headers != nil {
for key, value := range headers {
req.Header.Set(key, value)
}
if !slices.Contains(maps.Keys(headers), UserAgentHeaderKey) {
req.Header.Set(UserAgentHeaderKey, DefaultUserAgent)
}
} else {
req.Header.Set(UserAgentHeaderKey, DefaultUserAgent)
}
if body != nil {
b, _ := json.Marshal(body)
req.SetBody(b)
req.Header.SetContentType("application/json")
}
req.Header.SetMethod(method)
if clientToken != "" {
req.Header.Set(AuthorizationHeaderKey, "Bot "+clientToken)
}
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
err := fasthttp.Do(req, res)
if err != nil {
panic("Error in request: " + err.Error())
}
return res.Body()
}
// ApplicationCommandsBulkOverwrite Overwrite all application commands
// (In case guild id is not provided to edit commands globally)
func ApplicationCommandsBulkOverwrite(applicationID string, commands []*ApplicationCommand, guildID, clientToken string) (createdCommands []*ApplicationCommand, err error) {
uri := endpoints.FormatAPIURI(endpoints.ApplicationCommandsGlobal(applicationID))
if guildID != "" {
uri = endpoints.FormatAPIURI(endpoints.ApplicationCommandsGuild(applicationID, guildID))
}
res := Request(uri, fasthttp.MethodPut, commands, clientToken, nil)
err = json.Unmarshal(res, &createdCommands)
return
}
// CreateMessage Send a message to a channel
func CreateMessage(clientToken string, channelID string, messageData *Message) {
Request(endpoints.Messages(channelID), fasthttp.MethodPost, messageData, clientToken, nil)
}
// FetchMessage Fetch a message in channel
func FetchMessage(clientToken string, channelID, messageID string) {
Request(endpoints.Message(channelID, messageID), fasthttp.MethodGet, nil, clientToken, nil)
}
// DeleteMessage Delete a message in channel
func DeleteMessage(clientToken, channelID, messageID string) {
Request(endpoints.Message(channelID, messageID), fasthttp.MethodDelete, nil, clientToken, nil)
}
// EditMessage Edit a message in channel
func EditMessage(clientToken, channelID, messageID string, messageData *Message) {
Request(endpoints.Message(channelID, messageID), fasthttp.MethodPatch, messageData, clientToken, nil)
}
// CreateReaction Create a reaction in the message
func CreateReaction(clientToken, channelID, messageID, reaction string) {
Request(
endpoints.UserReaction(channelID, messageID, reaction, "@me"),
fasthttp.MethodPut,
nil, clientToken,
nil,
)
}
// RemoveReaction Remove a reaction from message
func RemoveReaction(clientToken, channelID, messageID, reaction, userID string) {
Request(
endpoints.UserReaction(channelID, messageID, reaction, userID),
fasthttp.MethodDelete,
nil, clientToken,
nil,
)
}
// RemoveAllReactions Remove all reactions from message
func RemoveAllReactions(clientToken, channelID, messageID string) {
Request(
endpoints.Reactions(channelID, messageID),
fasthttp.MethodDelete,
nil, clientToken,
nil,
)
}
// MessageReactions Get all reactions from message
func MessageReactions(clientToken, channelID, messageID string, options SearchQueryParams) {
var query string
if options.Limit != 0 {
query = fmt.Sprintf("?limit=%d", options.Limit)
}
if options.After.String() != "" {
if query != "" {
query += fmt.Sprintf("&after=%s", options.After.String())
} else {
query = fmt.Sprintf("?after=%s", options.After.String())
}
}
uri := endpoints.Reactions(channelID, messageID)
if query != "" {
uri += query
}
Request(
uri,
fasthttp.MethodGet,
nil, clientToken,
nil,
)
}
func EditOriginalInteractionResponse(applicationID, interactionToken string, data *WebhookEdit) {
Request(
endpoints.FormatAPIURI(endpoints.WebhookMessage(applicationID, interactionToken, "@original")),
fasthttp.MethodPatch,
data, "", nil,
)
}
func DeleteOriginalInteractionResponse(applicationID, interactionToken string) {
Request(
endpoints.FormatAPIURI(endpoints.WebhookMessage(applicationID, interactionToken, "@original")),
fasthttp.MethodDelete,
nil, "", nil,
)
}
func FollowUpInteractionResponse(applicationID, interactionToken string, data *WebhookEdit) {
Request(
endpoints.FormatAPIURI(endpoints.WebhookExecute(applicationID, interactionToken)),
fasthttp.MethodDelete,
data, "", nil,
)
}
type SearchQueryParams struct {
Limit int
After Snowflake
}