-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommand.go
117 lines (106 loc) · 3.48 KB
/
command.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/eleboucher/coalibot/citation"
"github.com/eleboucher/coalibot/fortyTwo"
"github.com/eleboucher/coalibot/miscs"
"github.com/eleboucher/coalibot/twitch"
"github.com/eleboucher/coalibot/users"
"github.com/eleboucher/coalibot/utils"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
var commands = map[string]func(string, *utils.Message) bool{
"hello": miscs.Hello,
"vdm": miscs.Vdm,
"roulette": miscs.Roulette,
"coin": miscs.Coin,
"meteo": miscs.Meteo,
"weather": miscs.Meteo,
"roll": miscs.Roll,
"roulettestat": miscs.RouletteStat,
"score": utils.FortyTwoMiddleware(fortyTwo.Score),
"alliance": utils.FortyTwoMiddleware(fortyTwo.Alliance),
"prof": utils.FortyTwoMiddleware(fortyTwo.Prof),
"logtime": utils.FortyTwoMiddleware(fortyTwo.Logtime),
"who": utils.FortyTwoMiddleware(fortyTwo.Who),
"where": utils.FortyTwoMiddleware(fortyTwo.Where),
"event": utils.FortyTwoMiddleware(fortyTwo.Event),
"oss": citation.Oss,
"kaamelott": citation.Kaamelott,
"mhenni": citation.Mhenni,
"glados": citation.GLaDOS,
"help": miscs.Help,
"music": miscs.Music,
"addmusic": miscs.AddMusic,
"dtc": miscs.Dtc,
"roulettetop": miscs.RouletteTop,
"anroche": users.Anroche,
"gfaim": miscs.Gfaim,
"apero": miscs.Apero,
"skin": miscs.Skin,
"emote": twitch.Emotes,
}
func handleCommand(event *utils.Message) {
var isCommand = false
var option string
var command string
sort.Strings(BlackList)
i := sort.Search(len(BlackList),
func(i int) bool { return BlackList[i] >= event.Channel })
if (i < len(BlackList) && BlackList[i] == event.Channel) &&
!(strings.Contains(strings.ToLower(event.Message), "bde") && event.Channel == "C04GT8U3Y") {
return
}
event.Message = strings.Join(strings.Fields(event.Message), " ")
splited := strings.Split(event.Message, " ")
if event.Message == "" {
return
}
if utils.IndexOf(strings.ToLower(splited[0]), []string{"coalibot", "bc", "cb"}) > -1 && len(splited) > 1 {
command = strings.ToLower(splited[1])
option = strings.Join(splited[2:], " ")
isCommand = reply(command, event)
if !isCommand && commands[command] != nil {
isCommand = commands[strings.ToLower(command)](option, event)
}
} else if splited[0][0] == '!' && len(splited[0]) > 1 {
command = strings.ToLower(splited[0][1:])
option = strings.Join(splited[1:], " ")
isCommand = reply(command, event)
if !isCommand && commands[command] != nil {
isCommand = commands[strings.ToLower(command)](option, event)
}
}
if isCommand {
log.WithFields(logrus.Fields{"Channel": event.Channel, "User": event.User, "command": event.Message}).Info()
}
}
func reply(command string, event *utils.Message) bool {
// Open our jsonFile
jsonFile, err := os.Open("reply.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
return false
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
// a map container to decode the JSON structure into
c := make(map[string]interface{})
// unmarshal JSON
e := json.Unmarshal(byteValue, &c)
if e != nil || c[command] == nil {
return false
}
// output result to STDOUT
fmt.Printf("reply %s\n", c[command].(string))
utils.PostMsg(event, slack.MsgOptionText(c[command].(string), false))
return true
}