-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
203 lines (169 loc) · 4.78 KB
/
utils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package discord
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/bwmarrin/discordgo"
)
const (
DiscordBaseURL = "https://discord.com/api"
ErrMissingRole = "You don't have the required role to use this command!"
)
type message struct {
Content string `json:"content"`
Components components `json:"components"`
}
type components struct {
Type int `json:"type"`
Components []button `json:"components"`
}
type button struct {
Type int `json:"type"`
Label string `json:"label"`
Style int `json:"style"`
CustomID string `json:"custom_id"`
}
type options struct {
actionsRow *discordgo.ActionsRow
// For tests
client *http.Client
url string
}
type Option func(*options)
func WithActionsRow(actionsRow *discordgo.ActionsRow) Option {
return func(o *options) {
o.actionsRow = actionsRow
}
}
func WithClient(client *http.Client) Option {
return func(o *options) {
o.client = client
}
}
func WithURL(url string) Option {
return func(o *options) {
o.url = url
}
}
func CheckRole(roles []string, requiredRole string) bool {
for _, role := range roles {
if role == requiredRole {
return true
}
}
return false
}
func DeferredMessage() (discordgo.InteractionResponse, error) {
return discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
}, nil
}
func SendDeferredMessage(appID string, token string, content string, opts ...Option) error {
log.Printf("Sending message: %s", content)
// Defaults
config := &options{
// For tests
client: &http.Client{},
url: DiscordBaseURL,
}
for _, opt := range opts {
opt(config)
}
payload, err := json.Marshal(map[string]string{
"content": content,
})
if err != nil {
return fmt.Errorf("couldn't marshal JSON: %v", err)
}
url := fmt.Sprintf("%v/v%v/webhooks/%v/%v", config.url, os.Getenv("DISCORD_API_VERSION"), appID, token)
// log.Printf("Discord API URL: %s", url)
request, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("error! couldn't create http request: %v", err)
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bot "+os.Getenv("DISCORD_BOT_TOKEN"))
client := config.client
response, err := client.Do(request)
if err != nil {
return fmt.Errorf("error! couldn't send the http request: %v", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
var result map[string]interface{}
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
log.Printf("Error! Couldn't decode result: %v", err)
return err
}
log.Printf("Error! Discord API Error: %v", result)
return fmt.Errorf("error! discord API error: %v", result)
}
return nil
}
func SendDeferredMessageWithComponents(appID string, token string, content string, opts ...Option) error {
log.Printf("Sending message: %s", content)
// Defaults
config := &options{
// For tests
client: &http.Client{},
url: DiscordBaseURL,
}
for _, opt := range opts {
opt(config)
}
message := message{
Content: content,
Components: components{},
}
if config.actionsRow != nil {
var buttons []button
for _, i := range config.actionsRow.Components {
buttons = append(buttons, button{
Type: int(i.Type()),
Label: i.(discordgo.Button).Label,
Style: int(i.(discordgo.Button).Style),
CustomID: i.(discordgo.Button).CustomID,
})
}
message.Components = components{
Type: int(discordgo.ActionsRowComponent),
Components: buttons,
}
}
payload, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("couldn't marshal JSON: %v", err)
}
temp, _ := json.Marshal(map[string]string{
"content": "test",
})
log.Printf("Correct Payload: %s", string(temp))
log.Printf("Payload: %s", string(payload))
url := fmt.Sprintf("%v/v%v/webhooks/%v/%v", config.url, os.Getenv("DISCORD_API_VERSION"), appID, token)
// log.Printf("Discord API URL: %s", url)
request, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("error! couldn't create http request: %v", err)
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bot "+os.Getenv("DISCORD_BOT_TOKEN"))
client := config.client
response, err := client.Do(request)
if err != nil {
return fmt.Errorf("error! couldn't send the http request: %v", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
var result map[string]interface{}
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
log.Printf("Error! Couldn't decode result: %v", err)
return err
}
log.Printf("Error! Discord API Error: %v", result)
return fmt.Errorf("error! discord API error: %v", result)
}
return nil
}