change cmd parser to discord package
This commit is contained in:
		
							parent
							
								
									8fe40c92f4
								
							
						
					
					
						commit
						bdd2e0b0cc
					
				@ -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) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										6
									
								
								pkg/svc/discord/actions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								pkg/svc/discord/actions.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
			
		||||
package discord
 | 
			
		||||
 | 
			
		||||
// SendDiscordMessage -
 | 
			
		||||
func SendDiscordMessage(channel string) (err error) {
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
@ -3,6 +3,7 @@ package discord
 | 
			
		||||
import (
 | 
			
		||||
	dsmodel "dorisbot/models/discord"
 | 
			
		||||
	pubmodel "dorisbot/models/public"
 | 
			
		||||
	"dorisbot/pkg/cmdparser"
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"github.com/bwmarrin/discordgo"
 | 
			
		||||
@ -36,36 +37,12 @@ func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var cmdmodel *pubmodel.CommandModel
 | 
			
		||||
	var acts []*cmdparser.CmdAction
 | 
			
		||||
 | 
			
		||||
	// discord server not init
 | 
			
		||||
	if server == nil {
 | 
			
		||||
		cmds, err := pubmodel.GetGlobalCommand(cmd)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("get command fail :: ", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		if len(cmds) == 0 {
 | 
			
		||||
			log.Printf("no command found {%s}\n", cmd)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		c := cmds[0]
 | 
			
		||||
		if c.RequireInit {
 | 
			
		||||
			log.Printf("{%s} require init server\n", cmd)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if c.RequireManage {
 | 
			
		||||
			ginfo, err := s.Guild(guildID)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println("get guild info err :: ", err)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			if ginfo.OwnerID != uid {
 | 
			
		||||
				log.Printf("{%s} require manage permission\n", cmd)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// TODO: command parser
 | 
			
		||||
		cmdmodel, acrs, err = getNotInitServerCommandAction(s, evt, cmd)
 | 
			
		||||
	} else {
 | 
			
		||||
		cmds, err := pubmodel.GetCommandByPlatformBinding(cmd, server.ID, "discord")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@ -112,9 +89,13 @@ func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// TODO: command parser
 | 
			
		||||
		cmdmodel = c
 | 
			
		||||
		acts = cmdparser.ParseCmdAction(cmdmodel.Value)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(acts) == 0 {
 | 
			
		||||
		// TODO: response cmd value
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func guildMemberAddEvt(s *discordgo.Session, evt *discordgo.GuildMemberAdd) {}
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,16 @@
 | 
			
		||||
package discord
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	pubmodel "dorisbot/models/public"
 | 
			
		||||
	"dorisbot/pkg/cmdparser"
 | 
			
		||||
 | 
			
		||||
	"github.com/bwmarrin/discordgo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var tagRagexp = regexp.MustCompile("<@([0-9]+)>")
 | 
			
		||||
@ -40,6 +48,41 @@ func getCMD(str string) (cmd string, keyword bool) {
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func parseTextMessage(msg string) (cmd string, keyword bool, payload string) {
 | 
			
		||||
	tagBot := getTagBot(msg)
 | 
			
		||||
	_ = tagBot
 | 
			
		||||
@ -53,3 +96,10 @@ func parseTextMessage(msg string) (cmd string, keyword bool, payload string) {
 | 
			
		||||
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func selectAction(key, value, payload string) {
 | 
			
		||||
	switch key {
 | 
			
		||||
	case "ds_addserver":
 | 
			
		||||
		break
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user