-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (145 loc) · 4.34 KB
/
main.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
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"github.com/bwmarrin/discordgo"
"moul.io/u"
)
func main() {
var (
token = os.Getenv("DISCORD_BOT_TOKEN")
guildID = os.Getenv("DISCORD_GUILD_ID")
apiEndpoint = os.Getenv("API_ENDPOINT")
)
s, err := discordgo.New("Bot " + token)
u.CheckErr(err)
// create tls client
var client *http.Client
{
//cert, err := tls.LoadX509KeyPair("usercert.pem", "userkey.pem")
//u.CheckErr(err)
//caCertPool := x509.NewCertPool()
//caCert, err := ioutil.ReadFile("cacert.pem")
//u.CheckErr(err)
//caCertPool.AppendCertsFromPEM(caCert)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
//Certificates: []tls.Certificate{cert},
//RootCAs: caCertPool,
},
}
client = &http.Client{Transport: tr}
}
var authz = "Basic " + u.B64Encode([]byte(os.Getenv("API_AUTHORIZATION")))
apiOpen := func() (string, error) {
form := url.Values{}
form.Add("action", "open")
form.Add("delay", "5")
req, err := http.NewRequest("POST", apiEndpoint+"/api.php", strings.NewReader(form.Encode()))
if err != nil {
return "", fmt.Errorf("creating new HTTP request: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", authz)
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("making HTTP request using client: %w", err)
}
respBody, _ := ioutil.ReadAll(resp.Body)
log.Printf("reply: %q", resp.Body)
return string(respBody), nil
}
apiStatus := func() (string, error) {
req, err := http.NewRequest("GET", apiEndpoint+"/api.php", nil)
if err != nil {
return "", fmt.Errorf("creating new HTTP request: %w", err)
}
req.Header.Add("Authorization", authz)
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("makging HTTP request using client: %w", err)
}
respBody, _ := ioutil.ReadAll(resp.Body)
log.Printf("reply: %q", resp.Body)
return string(respBody), nil
}
{
status, err := apiStatus()
u.CheckErr(err)
log.Printf("status on init: %q", status)
}
// commands
{
commands := []*discordgo.ApplicationCommand{
{Name: "sesame", Description: "ouvre toi !"},
{Name: "status", Description: "kezispass ?"},
}
commandHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"sesame": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
log.Println("/sesame called")
status, err := apiOpen()
if err != nil {
log.Println("error: %+v", err)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("ERROR: %q", err),
},
})
} else {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("NORMALEMENT C'EST BON: %q", status),
},
})
}
},
"status": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
log.Println("/status called")
status, err := apiStatus()
if err != nil {
log.Println("error: %+v", err)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("ERROR: %q", err),
},
})
} else {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("NORMALEMENT C'EST BON: %q", status),
},
})
}
},
}
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
u.CheckErr(s.Open())
defer s.Close()
for _, v := range commands {
_, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, v)
u.CheckErr(err)
}
}
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutdowning")
}