dorisbot/pkg/svc/discord/parser.go
2019-09-16 07:51:32 +00:00

182 lines
4.1 KiB
Go

package discord
import (
"errors"
"fmt"
"log"
"regexp"
"strings"
dsmodel "dorisbot/models/discord"
pubmodel "dorisbot/models/public"
"dorisbot/pkg/cmdparser"
"github.com/bwmarrin/discordgo"
)
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
}
func getRegServerCommandAction(s *discordgo.Session, msgEvt *discordgo.MessageCreate, server *dsmodel.ServerModel, cmd string) (c *pubmodel.CommandModel, acts []*cmdparser.CmdAction, err error) {
cmds, err := pubmodel.GetCommandByPlatformBinding(cmd, server.ID, "discord")
if err != nil {
log.Println("get command err :: ", err)
return
}
if len(cmds) == 0 {
log.Printf("no command found {%s}\n", cmd)
return
}
c = cmds[0]
if c.RequireManage && server.Owner != msgEvt.Author.ID {
roles, err := server.GetRoleList()
if err != nil {
log.Println("get server role err :: ", err)
return nil, nil, err
}
rolePass := false
if len(roles) > 0 {
uinfo, err := s.GuildMember(msgEvt.GuildID, msgEvt.Author.ID)
if err != nil {
log.Println("get guild user info err :: ", err)
return nil, nil, err
}
if len(uinfo.Roles) > 0 {
allPass := false
for _, it := range roles {
if allPass {
break
}
for _, rid := range uinfo.Roles {
if it.RoleID == rid {
rolePass = true
break
}
}
}
}
}
if !rolePass {
s := fmt.Sprintf("{%s} require manage role", cmd)
log.Println(s)
return nil, nil, errors.New(s)
}
}
acts = cmdparser.ParseCmdAction(c.Value)
return c, acts, nil
}
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(msgEvt.GuildID)
if err != nil {
log.Println("get guild info err :: ", err)
return nil, nil, err
}
if ginfo.OwnerID != msgEvt.Author.ID {
s := fmt.Sprintf("{%s} require manage permission", cmd)
log.Println(s)
return nil, nil, errors.New(s)
}
}
acts = cmdparser.ParseCmdAction(c.Value)
return c, acts, nil
}
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
}
func selectAction(info EvtInfo, cmdKey, cmdValue, payload string) string {
switch cmdKey {
case "ds_addserver":
return addServer(info, payload)
case "ds_addchannel":
strs := strings.Split(payload, " ")
if len(strs) != 2 {
return "!ds_addchennel {enable_cmd} {enable_notify}"
}
return addChannel(info, strs[0], strs[1])
case "ds_addfacebook":
strs := strings.Split(payload, " ")
if len(strs) < 2 {
return "!ds_addfacebook {facebook_id} {template}"
}
return addFacebookPage(info, strs[0], strings.Join(strs[1:], " "))
case "ds_delfacebook":
return delFacebookPage(info, payload)
case "ds_addinstagram":
strs := strings.Split(payload, " ")
if len(strs) < 2 {
return "!ds_addinstagram {instagram_id} {template}"
}
return addInstagram(info, strs[0], strings.Join(strs[1:], " "))
case "ds_delinstagram":
return delInstagram(info, payload)
}
return ""
}