From 8fe40c92f47e4ae41e4ccbc76f065149942ed764 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 3 Jul 2019 09:58:38 +0000 Subject: [PATCH] add cdmparser --- pkg/cmdparser/cmdparser.go | 59 ++++++++++++++++++++++++++++++++++++++ pkg/svc/discord/events.go | 1 - 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 pkg/cmdparser/cmdparser.go diff --git a/pkg/cmdparser/cmdparser.go b/pkg/cmdparser/cmdparser.go new file mode 100644 index 0000000..a42eb81 --- /dev/null +++ b/pkg/cmdparser/cmdparser.go @@ -0,0 +1,59 @@ +package cmdparser + +import "regexp" + +// 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 +} + +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) { + strs := actRegex.FindAllStringSubmatch(value, -1) + if len(strs) == 0 { + return nil + } + + acts = make([]*CmdAction, 0, len(strs)) + + for _, key := range strs { + acts = append(acts, &CmdAction{Key: key[1]}) + } + + return +} + +func selectAction(act *CmdAction) { +} diff --git a/pkg/svc/discord/events.go b/pkg/svc/discord/events.go index 3e1b6b7..51291f5 100644 --- a/pkg/svc/discord/events.go +++ b/pkg/svc/discord/events.go @@ -78,7 +78,6 @@ func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) { } c := cmds[0] - // TODO: check user role group if c.RequireManage && server.Owner != uid { roles, err := server.GetRoleList() if err != nil {