-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
108 lines (84 loc) · 2.26 KB
/
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
108
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"sort"
"github.com/diamondburned/arikawa/v3/discord"
)
type configuration struct {
Prefix string `json:"prefix"`
Token string `json:"token"`
Permissions commandPermissions `json:"permissions"`
Aliases map[string]string `json:"aliases"`
Blacklist map[discord.Snowflake]struct{} `json:"blacklist"`
}
// snowflakeLookup transforms a json list to a map for faster lookups
type snowflakeLookup map[discord.Snowflake]struct{}
func (c *snowflakeLookup) UnmarshalJSON(data []byte) error {
snowflakes := make([]discord.Snowflake, 0)
err := json.Unmarshal(data, &snowflakes)
if err != nil {
return err
}
*c = map[discord.Snowflake]struct{}{}
for _, s := range snowflakes {
(*c)[s] = struct{}{}
}
return nil
}
func (c snowflakeLookup) MarshalJSON() ([]byte, error) {
snowflakes := make([]discord.Snowflake, 0, len(c))
for snowflake := range c {
snowflakes = append(snowflakes, snowflake)
}
sort.Slice(snowflakes, func(i, j int) bool {
return snowflakes[i] < snowflakes[j]
})
return json.Marshal(snowflakes)
}
type commandPermissions struct {
Docs snowflakeLookup `json:"docs"`
Config map[discord.GuildID]snowflakeLookup `json:"config"`
}
func config() configuration {
fileBytes, err := os.ReadFile("config.json")
if err != nil {
log.Fatal(fmt.Errorf("could not open config, %w", err))
}
config, err := configFromBytes(fileBytes)
if err != nil {
log.Fatalf("could not parse config, %s", err)
}
if config.Aliases == nil {
config.Aliases = map[string]string{}
}
if config.Blacklist == nil {
config.Blacklist = map[discord.Snowflake]struct{}{}
}
return config
}
func configFromBytes(data []byte) (configuration, error) {
var config configuration
err := json.Unmarshal(data, &config)
if err != nil {
return config, err
}
if config.Aliases == nil {
config.Aliases = map[string]string{}
}
if config.Blacklist == nil {
config.Blacklist = map[discord.Snowflake]struct{}{}
}
return config, nil
}
func saveConfig(config configuration) error {
f, err := os.OpenFile("config.json", os.O_WRONLY, 0o644)
if err != nil {
return err
}
encoder := json.NewEncoder(f)
encoder.SetIndent("", "\t")
return encoder.Encode(config)
}