-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhook.go
More file actions
117 lines (100 loc) · 2.88 KB
/
hook.go
File metadata and controls
117 lines (100 loc) · 2.88 KB
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
package rhookie
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
const (
// baseURL is the base URL of the discord webhook API.
baseURL = "https://discord.com/api/webhooks/%s/%s"
// noExtraEndpoint is an endpoint that doesn't have any extra path.
noExtraEndpoint = ""
)
// Hook is a Discord webhook.
type Hook struct {
id, token string
}
// SendMessage sends a message to the webhook.
func (h Hook) SendMessage(payload Payload) error {
return h.doRequest(http.MethodPost, noExtraEndpoint, payload)
}
// SendMessageWithResponse sends a message to the webhook and returns the created message.
func (h Hook) SendMessageWithResponse(payload Payload) (Message, error) {
return h.doRequestWithResponse(http.MethodPost, "?wait=true", payload)
}
// EditMessage edits a message sent by the webhook.
func (h Hook) EditMessage(id string, payload Payload) error {
return h.doRequest(http.MethodPatch, "/messages/"+id, payload)
}
// doRequest sends a request to the webhook.
func (h Hook) doRequest(method, extraEndpoint string, payload Payload) error {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(payload)
if err != nil {
return err
}
req, err := http.NewRequest(method, h.buildURL(extraEndpoint), &buf)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("webhook request failed: %s: %s", resp.Status, strings.TrimSpace(string(body)))
}
return nil
}
func (h Hook) doRequestWithResponse(method, extraEndpoint string, payload Payload) (Message, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(payload)
if err != nil {
return Message{}, err
}
req, err := http.NewRequest(method, h.buildURL(extraEndpoint), &buf)
if err != nil {
return Message{}, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Message{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Message{}, err
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return Message{}, fmt.Errorf("webhook request failed: %s: %s", resp.Status, strings.TrimSpace(string(body)))
}
var message Message
if err := json.Unmarshal(body, &message); err != nil {
return Message{}, err
}
return message, nil
}
func (h Hook) buildURL(extraEndpoint string) string {
base := fmt.Sprintf(baseURL, h.id, h.token)
if extraEndpoint == "" {
return base
}
if strings.HasPrefix(extraEndpoint, "?") {
return base + extraEndpoint
}
return base + extraEndpoint
}
// NewHook creates a new webhook.
func NewHook(id, token string) Hook {
return Hook{
id: id,
token: token,
}
}