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
|
// Package discordwebhook is a simple low-level HTTP client wrapper around Discord webhooks.
package discordwebhook
import (
"bytes"
"encoding/json"
"net/http"
"within.website/x/web"
)
// Webhook is the parent structure fired off to Discord.
type Webhook struct {
Content string `json:"content,omitempty"`
Username string `json:"username,omitempty"`
AvatarURL string `json:"avatar_url"`
Embeds []Embeds `json:"embeds,omitempty"`
AllowedMentions map[string][]string `json:"allowed_mentions"`
}
// EmbedField is an individual field being embedded in a message.
type EmbedField struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}
// EmbedFooter is the message footer.
type EmbedFooter struct {
Text string `json:"text"`
IconURL string `json:"icon_url"`
}
// Embeds is a set of embed fields and a footer.
type Embeds struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
Author *EmbedAuthor `json:"author,omitempty"`
Fields []EmbedField `json:"fields,omitempty"`
Footer *EmbedFooter `json:"footer,omitempty"`
}
type EmbedAuthor struct {
Name string `json:"name"`
URL string `json:"url"`
IconURL string `json:"icon_url"`
}
// Send returns a request for a Discord webhook.
func Send(whurl string, w Webhook) *http.Request {
if len(w.Username) > 32 {
w.Username = w.Username[:32]
}
data, err := json.Marshal(&w)
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, whurl, bytes.NewBuffer(data))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
return req
}
// Validate validates the response from Discord.
func Validate(resp *http.Response) error {
if resp.StatusCode != http.StatusNoContent {
return web.NewError(http.StatusNoContent, resp)
}
return nil
}
|