-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
91 lines (81 loc) · 2.26 KB
/
commands.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
package main
import (
"strings"
dg "github.com/bwmarrin/discordgo"
)
var newMessageCommands = map[string]*command{}
var aliases = map[string]string{}
func parseCommand(s *dg.Session, m *dg.MessageCreate) {
if m.Content != "" && m.Content[0] == '$' {
message := m.Content[1:]
words := strings.Fields(message)
v, ok := newMessageCommands[words[0]]
if !ok {
if alias, ok1 := aliases[words[0]]; ok1 {
if val, ok2 := newMessageCommands[alias]; ok2 {
v = val
ok = true
}
}
}
if ok {
com := v
commandLength := 1
for _, word := range words[1:] {
if val, ok := com.subCommands[word]; ok {
com = val
commandLength++
} else {
break
}
}
com.mainCommand(s, m, words[commandLength:])
}
}
}
func addCommand(call string, function func(s *dg.Session, m *dg.MessageCreate, args []string)) {
com := command{function, make(map[string]*command)}
com.add(call)
}
func addSubCommand(call string, function func(s *dg.Session, m *dg.MessageCreate, args []string), parentCommand string) {
words := strings.Fields(parentCommand)
if v, ok := newMessageCommands[words[0]]; ok {
com := v
commandLength := 1
for _, word := range words[1:] {
if _, ok := com.subCommands[word]; ok {
com = com.subCommands[word]
commandLength++
} else {
break
}
}
com.addSubCommand(call, function)
}
}
func addAlias(alias string, fullName string) {
aliases[alias] = fullName
}
func help(s *dg.Session, m *dg.MessageCreate, args []string) {
out := "The following commands are available:\n"
for k := range newMessageCommands {
out += "$" + k + "\n"
}
s.ChannelMessageSend(m.ChannelID, out)
}
func commandSetup() {
addCommand("gamehelp", help)
addCommand("tictactoe", tictactoe)
addAlias("ttt", "tictactoe")
addSubCommand("start", start, "tictactoe")
addSubCommand("cancelRequest", cancelRequest, "tictactoe")
addSubCommand("accept", accept, "tictactoe")
addSubCommand("concede", concede, "tictactoe")
addSubCommand("play", playttt, "tictactoe")
addCommand("higherorlower", higherorlower)
addAlias("hol", "higherorlower")
addSubCommand("show", show, "higherorlower")
addSubCommand("play", playhol, "higherorlower")
addSubCommand("highscore", highscore, "higherorlower")
addSubCommand("streak", streak, "higherorlower")
}