-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonParser.go
102 lines (90 loc) · 2.3 KB
/
jsonParser.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"unicode"
)
type dsgRole struct { // To hold calls and the role those calls affect
Calls []string `json:"calls"`
Role string `json:"role"`
Locked bool `json:"locked"`
}
type dsgMessage struct { // To hold a message and a channel to send when using dsgMessage.sendMessage()
ID string `json:"ID"`
Message string `json:"message"`
Type string `json:"type"`
}
type dsgServer struct {
ServerID string `json:"serverID"`
Roles []dsgRole `json:"roles"`
WelcomeMessage dsgMessage `json:"welcomeMessage"`
GoodbyeMessage dsgMessage `json:"goodbyeMessage"`
BanMessage dsgMessage `json:"banMessage"`
}
type dsgConfig struct {
Files []dsgFile `json:"files"`
}
type dsgFile struct {
ID string `json:"ID"`
Location string `json:"location"`
}
func loadConfig(l string) dsgConfig { // Gets the config file and unmarshals it into a dsgConfig struct
var cfg dsgConfig
raw, err := ioutil.ReadFile(l)
if err != nil {
fmt.Println("json file not found!, ", err.Error())
var r dsgConfig
return r
}
json.Unmarshal(raw, &cfg)
return cfg
}
func loadServers(c dsgConfig) []dsgServer { // Gets all servers added to a certain config file
servers := make([]dsgServer, len(c.Files))
for i := 0; i < len(c.Files); i++ {
var s dsgServer
raw, err := ioutil.ReadFile(c.Files[i].Location)
if err != nil {
fmt.Println("json file not found!, ", err.Error())
var r []dsgServer
return r
}
json.Unmarshal(raw, &s)
servers = append(servers, s)
}
return servers
}
func loadServer(c dsgConfig, ID string) dsgServer { // Gets a specific server based off of guild ID
i := 0
for i = 0; i < len(c.Files); i++ {
if c.Files[i].ID == ID {
var s dsgServer
raw, err := ioutil.ReadFile(c.Files[i].Location)
if err != nil {
fmt.Println("json file not found!, ", err.Error())
var r dsgServer
return r
}
err = json.Unmarshal(raw, &s)
if err != nil {
fmt.Println("Error unmarshal-ing JSON file, ", err.Error())
var r dsgServer
return r
}
return s
}
}
var r dsgServer
return r
}
//SpaceMap ... removes all whitespace from a string efficiently
func SpaceMap(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, str)
}