-
Notifications
You must be signed in to change notification settings - Fork 11
/
guilddb.go
172 lines (155 loc) · 3.53 KB
/
guilddb.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
package guilddb
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/xackery/talkeq/config"
"github.com/xackery/talkeq/tlog"
)
var (
isStarted bool
guilds map[int]string
mu sync.RWMutex
guildsDatabasePath string
)
// New creates a new guild database
func New(config *config.Config) error {
if isStarted {
return fmt.Errorf("already started")
}
guildsDatabasePath = config.GuildsDatabasePath
tlog.Debugf("[guilddb] initializing")
_, err := os.Stat(guildsDatabasePath)
if os.IsNotExist(err) {
err = ioutil.WriteFile(guildsDatabasePath, []byte(`# guildid:channelid #comment`), 0644)
if err != nil {
return fmt.Errorf("guilds database create %w", err)
}
}
err = reload()
if err != nil {
return fmt.Errorf("reload: %w", err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("newWatcher: %w", err)
}
err = watcher.Add(config.GuildsDatabasePath)
if err != nil {
return fmt.Errorf("watcherAdd: %w", err)
}
go loop(watcher)
return nil
}
func loop(watcher *fsnotify.Watcher) {
if isStarted {
return
}
mu.Lock()
isStarted = true
mu.Unlock()
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
tlog.Warn("[guilddb] failed to read file")
return
}
if event.Op&fsnotify.Write != fsnotify.Write {
continue
}
tlog.Debugf("[guilddb] modified, reloading")
err := reload()
if err != nil {
tlog.Warnf("[guilddb] failed to reload: %s", err)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
tlog.Warnf("[guilddb] failed to read file: %s", err)
}
}
}
func reload() error {
mu.Lock()
defer mu.Unlock()
data, err := ioutil.ReadFile(guildsDatabasePath)
if err != nil {
return fmt.Errorf("readFile: %w", err)
}
ng := make(map[int]string)
lines := strings.Split(string(data), "\n")
for lineNumber, line := range lines {
lineNumber++
line = strings.TrimSpace(line)
if len(line) < 1 {
//tlog.Debugf("[guilddb] line %d skipped, empty", lineNumber)
continue
}
if line[0] == '#' {
continue
}
p := strings.Index(line, ":")
if p < 1 {
tlog.Debugf("[guilddb] line %d skipped, no : found", lineNumber)
continue
}
sid := line[0:p]
if len(sid) < 1 {
tlog.Warnf("[guilddb] line %d failed, guildid too short", lineNumber)
continue
}
iid, err := strconv.Atoi(sid)
if err != nil {
tlog.Warnf("[guilddb] line %d failed, guildid not a valid integer", lineNumber)
continue
}
id := int(iid)
name := line[p+1:]
if len(name) < 3 {
tlog.Warnf("[guilddb] line %d failed, guildname too short", lineNumber)
continue
}
p = strings.Index(name, "#")
if p > 0 {
name = name[0:p]
}
name = strings.TrimSpace(name)
_, ok := ng[id]
if ok {
tlog.Debugf("[guilddb] line %d skipped, guildID %d is a duplicate entry", lineNumber, id)
}
ng[id] = name
}
guilds = ng
return nil
}
// Set updates or adds an entry for a specified guild id
func Set(guildID int, guildName string) {
mu.Lock()
defer mu.Unlock()
guilds[guildID] = guildName
}
// ChannelID returns the discord ChannelID of a guild based on their ID
func ChannelID(guildID int) string {
mu.RLock()
defer mu.RUnlock()
return guilds[guildID]
}
// GuildId returns the EQ guildID of a guild based on a provided discord channelID, returns 0 if no results
func GuildID(channelID string) int {
mu.RLock()
defer mu.RUnlock()
for guildID, gChannelID := range guilds {
if channelID == gChannelID {
return guildID
}
}
return 0
}