dorisbot/pkg/svc/discord/parser.go

106 lines
2.0 KiB
Go
Raw Normal View History

2019-06-28 09:37:37 +00:00
package discord
import (
2019-07-04 09:00:16 +00:00
"errors"
"fmt"
"log"
2019-06-28 09:37:37 +00:00
"regexp"
"strings"
2019-07-04 09:00:16 +00:00
pubmodel "dorisbot/models/public"
"dorisbot/pkg/cmdparser"
"github.com/bwmarrin/discordgo"
2019-06-28 09:37:37 +00:00
)
var tagRagexp = regexp.MustCompile("<@([0-9]+)>")
func getTagBot(msg string) bool {
ids := tagRagexp.FindAllString(msg, -1)
if len(ids) == 0 {
return false
}
for _, id := range ids {
if id == client.ID {
return true
}
}
return false
}
func getCMD(str string) (cmd string, keyword bool) {
cmd = ""
keyword = false
charNumber := []rune(str)[0]
if charNumber == 33 || charNumber == 65218 {
keyword = true
if charNumber == 65218 {
cmd = str[3:]
} else {
cmd = str[1:]
}
} else {
cmd = str
}
return
}
2019-07-04 09:00:16 +00:00
func getNotInitServerCommandAction(s discordgo.Session, msgEvt *discordgo.MessageCreate, cmd string) (c *pubmodel.CommandModel, acts []*cmdparser.CmdAction, err error) {
cmds, err := pubmodel.GetGlobalCommand(cmd)
if err != nil {
log.Println("get command fail :: ", err)
return nil, nil, err
}
if len(cmds) == 0 {
s := fmt.Sprintf("no command found {%s}", cmd)
log.Println(s)
return nil, nil, errors.New(s)
}
c := cmds[0]
if c.RequireInit {
s := fmt.Sprintf("{%s} require init server", cmd)
log.Println(s)
return nil, nil, errors.New(s)
}
if c.RequireManage {
ginfo, err := s.Guild(guildID)
if err != nil {
log.Println("get guild info err :: ", err)
return nil, nil, err
}
if ginfo.OwnerID != uid {
s := fmt.Sprintf("{%s} require manage permission", cmd)
log.Println(s)
return nil, nil, errors.New(s)
}
}
acts = cmdparser.ParseCmdAction(cmdmodel.Value)
return c, acts, nil
}
2019-06-28 09:37:37 +00:00
func parseTextMessage(msg string) (cmd string, keyword bool, payload string) {
tagBot := getTagBot(msg)
_ = tagBot
msg = strings.TrimSpace(tagRagexp.ReplaceAllString(msg, ""))
strs := strings.Split(msg, " ")
cmd, keyword = getCMD(strs[0])
payload = strings.Join(strs[1:], " ")
return
}
2019-07-04 09:00:16 +00:00
func selectAction(key, value, payload string) {
switch key {
case "ds_addserver":
break
}
}