-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_config.go
107 lines (94 loc) · 2.48 KB
/
load_config.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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
type DiscordConfig struct {
Webhook string `json:"webhook"`
RoleID uint64 `json:"role_id"`
BoardID uint64 `json:"board_id"`
}
type Config struct {
Name string `json:"name"`
VoteDescription string `json:"vote_description"`
ApplicationDescription string `json:"application_description"`
Positions []Position `json:"positions"`
}
type Position struct {
Name string `json:"name"`
Description string `json:"description"`
}
var eligibleApplicants []string
var eligibleVoters []string
var electionConfig Config
var discordConfig DiscordConfig
func init() {
appBytes, err := ioutil.ReadFile("config/applicants.txt")
if err != nil {
panic(err)
}
eligibleApplicants = strings.Split(string(appBytes), "\n")
for i, eligibleApplicant := range eligibleApplicants {
eligibleApplicants[i] = strings.TrimSpace(eligibleApplicant)
}
votersBytes, err := ioutil.ReadFile("config/voters.txt")
if err != nil {
panic(err)
}
eligibleVoters = strings.Split(string(votersBytes), "\n")
for i, eligibleVoter := range eligibleVoters {
eligibleVoters[i] = strings.TrimSpace(eligibleVoter)
}
configBytes, err := ioutil.ReadFile("config/positions.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(configBytes, &electionConfig)
if err != nil {
panic(err)
}
discordBytes, err := ioutil.ReadFile("config/discord.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(discordBytes, &discordConfig)
if err != nil {
panic(err)
}
}
type DiscordField struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}
type DiscordEmbed struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Color uint32 `json:"color"`
Fields []*DiscordField `json:"fields"`
}
func sendWebhookEmbed(text string, embed *DiscordEmbed) {
req, err := json.Marshal(map[string]interface{}{
"username": "Election Bot",
"content": text,
"embeds": []*DiscordEmbed{embed},
})
if err != nil {
panic(err)
}
http.Post(discordConfig.Webhook, "application/json", bytes.NewReader(req))
}
func sendWebhook(text string) {
req, err := json.Marshal(map[string]interface{}{
"username": "Election Bot",
"content": text,
})
if err != nil {
panic(err)
}
http.Post(discordConfig.Webhook, "application/json", bytes.NewReader(req))
}