-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalls.go
59 lines (49 loc) · 1.22 KB
/
halls.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
package judgego
import (
"bytes"
"encoding/json"
"log"
"sync"
)
const reactionHistoryFilename = "reactionHistory.json"
type inductionMap struct {
sync.RWMutex
m map[string]bool
}
var reactionHistory = loadReactionHistory()
func alreadyInducted(channelID, messageID string) bool {
reactionHistory.RLock()
defer reactionHistory.RUnlock()
if _, ok := reactionHistory.m[channelID+messageID]; ok {
return true
}
return false
}
func inductMessage(channelID, messageID string) {
reactionHistory.Lock()
defer reactionHistory.Unlock()
reactionHistory.m[channelID+messageID] = true
saveReactionHistory()
}
func loadReactionHistory() inductionMap {
reactionMap := make(map[string]bool)
// TODO: Just assuming a failure here means the file doesn't exist in s3 for now. Should handle situation where it actually fails.
b, err := getFromS3(reactionHistoryFilename)
if err != nil {
return inductionMap{m: reactionMap}
}
err = json.Unmarshal(b, &reactionMap)
if err != nil {
log.Fatal(err)
}
return inductionMap{m: reactionMap}
}
func saveReactionHistory() error {
b, err := json.Marshal(reactionHistory.m)
if err != nil {
log.Fatal(err)
}
buf := bytes.NewBuffer(b)
writeToS3(buf, reactionHistoryFilename)
return nil
}