-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreaction.go
47 lines (42 loc) · 1.19 KB
/
reaction.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"github.com/eleboucher/coalibot/utils"
"github.com/slack-go/slack"
)
func InitReaction() []utils.React {
file, err := os.OpenFile("reaction.json", os.O_WRONLY|os.O_CREATE, 0660)
if err != nil {
fmt.Println(err)
return nil
}
defer file.Close()
byteValue, _ := ioutil.ReadFile("reaction.json")
var reactions []utils.React
json.Unmarshal(byteValue, &reactions)
for i := 0; i < len(reactions); i++ {
reactions[i].Compiled, _ = regexp.Compile(fmt.Sprintf("(?i)(^|[^a-zA-Z0-9])(%s)($|[^a-zA-Z0-9])", reactions[i].Match))
}
return reactions
}
func reacts(event utils.Message, reaction utils.React, msgRef slack.ItemRef) {
for i := 0; i < len(reaction.Reactions); i++ {
event.API.AddReaction(reaction.Reactions[i], msgRef)
}
}
func React(event utils.Message, reactions []utils.React) {
msgRef := slack.NewRefToMessage(event.Channel, event.Timestamp)
for i := 0; i < len(reactions); i++ {
if reactions[i].Compiled.FindStringIndex(event.Message) != nil {
if reactions[i].Reaction != "" {
go event.API.AddReaction(reactions[i].Reaction, msgRef)
} else {
go reacts(event, reactions[i], msgRef)
}
}
}
}