-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook.go
112 lines (93 loc) · 2.52 KB
/
webhook.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"runtime/debug"
)
func SEND_ADMIN_ALERT(msg string) {
defer func() {
if r := recover(); r != nil {
log.Println(r, string(debug.Stack()))
}
}()
message := Message{
Username: "ADMIN",
Content: msg + " <@&1032717037568540792>",
}
err := SendMessage(WEBHOOK, message)
if err != nil {
log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DISCORD MSG ERROR !!!!!!!!!!!!!!!!!!", err)
}
}
func SendMessage(url string, message Message) error {
defer func() {
if r := recover(); r != nil {
log.Println(r, string(debug.Stack()))
}
}()
payload := new(bytes.Buffer)
err := json.NewEncoder(payload).Encode(message)
if err != nil {
return err
}
resp, err := http.Post(url, "application/json", payload)
if err != nil {
return err
}
if resp.StatusCode != 200 && resp.StatusCode != 204 {
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf(string(responseBody))
}
return nil
}
type Message struct {
Username string `json:"username,omitempty"`
AvatarUrl string `json:"avatar_url,omitempty"`
Content string `json:"content,omitempty"`
Embeds []Embed `json:"embeds,omitempty"`
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty`
}
type Embed struct {
Title string `json:"title,omitempty"`
Url string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Color string `json:"color,omitempty"`
Author Author `json:"author,omitempty"`
Fields []Field `json:"fields,omitempty"`
Thumbnail Thumbnail `json:"thumbnail,omitempty"`
Image Image `json:"image,omitempty"`
Footer Footer `json:"footer,omitempty"`
}
type Author struct {
Name string `json:"name,omitempty"`
Url string `json:"url,omitempty"`
IconUrl string `json:"icon_url,omitempty"`
}
type Field struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
Inline bool `json:"inline,omitempty"`
}
type Thumbnail struct {
Url string `json:"url,omitempty"`
}
type Image struct {
Url string `json:"url,omitempty"`
}
type Footer struct {
Text string `json:"text,omitempty"`
IconUrl string `json:"icon_url,omitempty"`
}
type AllowedMentions struct {
Parse []string `json:"parse,omitempty"`
Users []string `json:"users,omitempty"`
Roles []string `json:"roles,omitempty"`
}