-
Notifications
You must be signed in to change notification settings - Fork 2
/
discord.go
121 lines (99 loc) · 2.64 KB
/
discord.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"go.mlcdf.fr/sally/build"
)
type discordClient struct {
WebhookURL string
}
var _ io.Writer = (*discordClient)(nil)
// Webhook is the webhook object sent to discord
type Webhook struct {
Username string `json:"username"`
AvatarURL string `json:"avatar_url"`
Content string `json:"content"`
Embeds []Embed `json:"embeds"`
}
// Embed is the embed object
type Embed struct {
Author Author `json:"author"`
Title string `json:"title"`
URL string `json:"url"`
Description string `json:"description"`
Color int64 `json:"color"`
Fields []Field `json:"fields"`
Thumbnail Image `json:"thumbnail"`
Image Image `json:"image"`
Footer Footer `json:"footer"`
TimeStamp string `json:"timestamp"`
}
// Author is the author object
type Author struct {
Name string `json:"name"`
URL string `json:"url"`
IconURL string `json:"icon_url"`
}
// Field is the field object inside an embed
type Field struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline,omitempty"`
}
// Footer is the footer of the embed
type Footer struct {
Text string `json:"text"`
IconURL string `json:"icon_url"`
}
// Image is an image possibly contained inside the embed
type Image struct {
URL string `json:"url"`
}
func (c *discordClient) postInfo(webhook *Webhook) error {
webhook.Embeds[0].Color = 2201331
return c.post(webhook)
}
func (c *discordClient) postError(webhook *Webhook) error {
webhook.Embeds[0].Color = 15092300
return c.post(webhook)
}
func (c *discordClient) postSuccess(webhook *Webhook) error {
webhook.Embeds[0].Color = 5747840
return c.post(webhook)
}
func (c *discordClient) post(webhook *Webhook) error {
payload, err := json.Marshal(webhook)
if err != nil {
return fmt.Errorf("failed to marshal webhook payload: %s", err)
}
res, err := defaultHTTP.Post(c.WebhookURL, "application/json", bytes.NewReader(payload))
if err != nil {
return fmt.Errorf("failed to post to webhook: %s", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("failed read body response : %s", err)
}
if res.StatusCode >= 400 {
return fmt.Errorf("failed to post to webhook: reason=%s status=%s", body, res.Status)
}
return nil
}
func (c *discordClient) Write(p []byte) (n int, err error) {
hostname, err := os.Hostname()
if err != nil {
hostname = "(unknown)"
}
w := &Webhook{
Username: build.String(),
Embeds: []Embed{{Footer: Footer{Text: hostname}}},
}
w.Embeds[0] = Embed{
Description: string(p),
}
return len(p), c.postError(w)
}