-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (156 loc) · 4.2 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
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
204
205
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"gopkg.in/yaml.v2"
)
var (
config Config
filePath string
shardCount int
verbose bool
ErrNoConfig = errors.New("")
Version = "0.3.3"
)
const (
defaultBodyPattern = "{\"guild_count\": @guild_count@}"
)
type Config struct {
BotToken string `yaml:"botToken"`
Websites []Website `yaml:"websites"`
}
type Website struct {
Name string `yaml:"name"`
ApiPath string `yaml:"apiPath"`
Token string `yaml:"token"`
BodyPattern string `yaml:"bodyPattern"`
Method string `yaml:"method"`
}
type ApplicationResponse struct {
ID string `json:"id"`
Name string `json:"name"`
GuildCount *int `json:"approximate_guild_count"`
}
func buildBodyReader(website Website, application ApplicationResponse) io.Reader {
var pattern string
if len(website.BodyPattern) == 0 {
pattern = defaultBodyPattern
} else {
pattern = website.BodyPattern
}
pattern = strings.ReplaceAll(pattern, "@server_count@", strconv.Itoa(*application.GuildCount))
pattern = strings.ReplaceAll(pattern, "@shard_count@", strconv.Itoa(shardCount))
return strings.NewReader(pattern)
}
func init() {
flag.StringVar(&filePath, "config", "./config.yaml", "Path to the config file.")
flag.IntVar(&shardCount, "shards", 0, "The shard count.")
flag.BoolVar(&verbose, "verbose", false, "Print Application info.")
flag.Parse()
filename, err := filepath.Abs(filePath)
if err != nil {
log.Fatal(err)
}
yamlFile, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatal(err)
}
if len(config.BotToken) == 0 {
log.Fatal("botToken mus be defined on config.yaml")
}
if len(config.Websites) < 1 {
log.Fatal("No websites on config")
}
for i, website := range config.Websites {
if website.Name == "" {
log.Fatal("Must define a name to website")
}
if website.ApiPath == "" {
log.Fatalf("Must define a apiPath for website %s", website.Name)
}
if website.Token == "" {
log.Fatalf("Must define a token for website %s", website.Name)
}
if website.Method == "" {
config.Websites[i].Method = "POST"
}
}
}
func postStatsToWebsite(wg *sync.WaitGroup, website Website, application ApplicationResponse) {
defer wg.Done()
var req *http.Request
if strings.Contains(website.ApiPath, "@guild_count@") {
website.ApiPath = strings.ReplaceAll(website.ApiPath, "@guild_count@", fmt.Sprint(application.GuildCount))
website.ApiPath = strings.ReplaceAll(website.ApiPath, "@bot_id@", application.ID)
r, err := http.NewRequest(strings.ToUpper(website.Method), website.ApiPath, nil)
if err != nil {
log.Fatal(err)
}
req = r
} else {
body := buildBodyReader(website, application)
r, err := http.NewRequest(strings.ToUpper(website.Method), website.ApiPath, body)
if err != nil {
log.Fatal(err)
}
r.Header.Set("Content-Type", "application/json")
req = r
}
req.Header.Add("Authorization", website.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
log.Printf("• %s (%s): %s\n", website.Name, website.ApiPath, resp.Status)
}
func getApplicationInfo(botToken string) (ApplicationResponse, error) {
var body ApplicationResponse
req, err := http.NewRequest("GET", "https://discord.com/api/v10/applications/@me", nil)
if err != nil {
return body, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bot %s", botToken))
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return body, err
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return body, err
}
return body, nil
}
func main() {
var wg sync.WaitGroup
application, err := getApplicationInfo(config.BotToken)
if err != nil {
log.Fatal(err)
}
log.Printf("Application info:\n")
log.Printf("• ID: %s", application.ID)
log.Printf("• Name: %s", application.Name)
log.Printf("• Guild count: %d", *application.GuildCount)
if verbose {
return
}
for _, website := range config.Websites {
wg.Add(1)
go postStatsToWebsite(&wg, website, application)
}
wg.Wait()
}