change cmd parser to discord package

This commit is contained in:
Jay
2019-07-04 09:00:16 +00:00
parent 8fe40c92f4
commit bdd2e0b0cc
4 changed files with 87 additions and 64 deletions
+21 -35
View File
@@ -1,46 +1,24 @@
package cmdparser
import "regexp"
import (
"regexp"
"strings"
)
// CommandType -
type CommandType int
// Cmd Types
const (
TextCmd CommandType = 1 << iota
ImageCmd
VideoCmd
)
// Command -
type Command struct {
Type CommandType
Message string
}
// CmdAction -
type CmdAction struct {
Key string
Act string
Key string
Value string
Origin string
}
var actRegex = regexp.MustCompile("{{(.+?)}}")
// CommandParser -
func CommandParser(value string) (cmd *Command, err error) {
acts := parseCmdAction(value)
if len(acts) == 0 {
// no get actions return origin value
return &Command{
Type: TextCmd,
Message: value,
}, nil
}
return
}
func parseCmdAction(value string) (acts []*CmdAction) {
// ParseCmdAction -
func ParseCmdAction(value string) (acts []*CmdAction) {
strs := actRegex.FindAllStringSubmatch(value, -1)
if len(strs) == 0 {
return nil
@@ -49,11 +27,19 @@ func parseCmdAction(value string) (acts []*CmdAction) {
acts = make([]*CmdAction, 0, len(strs))
for _, key := range strs {
acts = append(acts, &CmdAction{Key: key[1]})
act := &CmdAction{}
act.Origin = key[1]
keys := strings.Split(key[1], "=")
if len(keys) == 2 {
act.Key = keys[0]
act.Value = keys[1]
} else {
act.Key = key[1]
}
acts = append(acts, act)
}
return
}
func selectAction(act *CmdAction) {
}