-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_request.go
44 lines (37 loc) · 1.13 KB
/
api_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
package discordtexthook
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
func sendAPIRequest(message, messageID, todo string, ds discord) (*http.Response, error) {
var method, apiURL string
switch todo {
case "send":
method = "POST"
apiURL = "https://discord.com/api/v9/webhooks/" + ds.WebhookID + "/" + ds.WebhookToken + "?wait=true"
case "edit":
method = "PATCH"
apiURL = "https://discord.com/api/v9/webhooks/" + ds.WebhookID + "/" + ds.WebhookToken + "/messages/" + messageID
case "delete":
method = "DELETE"
apiURL = "https://discord.com/api/v9/webhooks/" + ds.WebhookID + "/" + ds.WebhookToken + "/messages/" + messageID
}
payload := map[string]string{"content": message}
byteValue, err := json.Marshal(payload)
if err != nil {
return nil, err
}
pload := bytes.NewBuffer(byteValue)
return executeRequest(method, apiURL, pload)
}
func executeRequest(method, apiURL string, payload io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, apiURL, payload)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}