add commad sys

This commit is contained in:
Jay 2019-07-09 15:05:36 +00:00
parent bdd2e0b0cc
commit 7cc675bfda
6 changed files with 140 additions and 66 deletions

View File

@ -84,6 +84,7 @@ func (p *DiscordServerModel) Write() error {
if err != nil {
return err
}
row.Next()
err = row.StructScan(p)
if err != nil {
return err

View File

@ -21,7 +21,7 @@ type CommandModel struct {
// NewCommand -
func NewCommand(key, value, platform, binding string, requireManage, requireInit bool) (c *CommandModel, err error) {
if len(key) == 0 || len(value) == 0 {
return nil, errors.New("cmd, server or value is empty")
return nil, errors.New("key, server or value is empty")
}
c = &CommandModel{
@ -100,10 +100,10 @@ func GetGlobalCommand(key string) (list []*CommandModel, err error) {
return nil, errors.New("key is empty")
}
db := models.GetConn()
query := `select "cmd", "server", "value", "require_manage", "require_init", "ctime", "mtime"
query := `select "key", "platform", "binding", "value", "require_manage", "require_init", "ctime", "mtime"
from "command"
where
"cmd" = $1
"key" = $1
and "platform" = ''
and "binding" = ''`
err = db.Select(&list, query, key)

View File

@ -1,6 +1,39 @@
package discord
// SendDiscordMessage -
func SendDiscordMessage(channel string) (err error) {
return
import (
dsmodel "dorisbot/models/discord"
"log"
)
func addServer(info EvtInfo, name string) string {
exists, err := dsmodel.GetDiscordServerByID(info.Server)
if err != nil {
log.Println("check server exists fail :: ", err)
return "check server info fail"
}
if exists != nil {
return "server already setup"
}
ds, err := dsmodel.NewDiscordServer(info.Server, name, 0)
if err != nil {
log.Println("create dsmodel fail :: ", err)
return "create server data fail"
}
log.Println("show info :: ", info)
owner, err := client.GetGuildOwner(info.Server)
if err != nil {
log.Println("get server owner err :: ", err)
return "get server owner fail"
}
ds.Owner = owner
err = ds.Write()
if err != nil {
log.Println("write server data fail :: ", err)
return "write server data fail"
}
return "add server success"
}

View File

@ -51,6 +51,9 @@ func NewClient(token string) (*DiscordClient, error) {
return client, nil
}
// GetClient -
func GetClient() *DiscordClient { return client }
// Connect to discord websocket
func (p *DiscordClient) Connect() error {
if p.connected == true {
@ -69,7 +72,7 @@ func (p *DiscordClient) Close() error {
// GetGuildOwner -
func (p *DiscordClient) GetGuildOwner(guild string) (ownerID string, err error) {
if len(ownerID) == 0 {
if len(guild) == 0 {
return "", ErrGuildIDEmpty
}
guildInfo, err := p.Session.Guild(guild)
@ -110,7 +113,8 @@ func (p *DiscordClient) GetGuildRoles(guild string) (roles []GuildRole, err erro
}
// SendMessage -
func (p *DiscordClient) SendMessage() error {
return nil
func (p *DiscordClient) SendMessage(channel, message string) error {
var err error
_, err = p.Session.ChannelMessageSend(channel, message)
return err
}

View File

@ -4,11 +4,20 @@ import (
dsmodel "dorisbot/models/discord"
pubmodel "dorisbot/models/public"
"dorisbot/pkg/cmdparser"
"fmt"
"log"
"strings"
"github.com/bwmarrin/discordgo"
)
// EvtInfo - event source info
type EvtInfo struct {
Server string
Channel string
User string
}
func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
// skip bot message
if evt.Author.ID == client.ID {
@ -42,60 +51,33 @@ func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
// discord server not init
if server == nil {
cmdmodel, acrs, err = getNotInitServerCommandAction(s, evt, cmd)
cmdmodel, acts, err = getNotInitServerCommandAction(s, evt, cmd)
} else {
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 != uid {
roles, err := server.GetRoleList()
if err != nil {
log.Println("get server role err :: ", err)
return
}
rolePass := false
if len(roles) > 0 {
uinfo, err := s.GuildMember(guildID, uid)
if err != nil {
log.Println("get guild user info err :: ", err)
return
}
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 {
log.Printf("{%s} require manage role\n", cmd)
return
}
}
cmdmodel = c
acts = cmdparser.ParseCmdAction(cmdmodel.Value)
cmdmodel, acts, err = getRegServerCommandAction(s, evt, server, cmd)
}
if len(acts) == 0 {
// TODO: response cmd value
if cmdmodel == nil {
log.Println("cmd is nil")
return
}
client.SendMessage(channelID, cmdmodel.Value)
return
}
info := EvtInfo{
Server: guildID,
Channel: channelID,
User: uid,
}
for _, it := range acts {
res := selectAction(info, it.Key, it.Value, payload)
// TODO: command result parse message type
cmdmodel.Value = strings.ReplaceAll(cmdmodel.Value, fmt.Sprintf("{{%s}}", it.Origin), res)
}
client.SendMessage(channelID, cmdmodel.Value)
}
func guildMemberAddEvt(s *discordgo.Session, evt *discordgo.GuildMemberAdd) {}

View File

@ -7,6 +7,7 @@ import (
"regexp"
"strings"
dsmodel "dorisbot/models/discord"
pubmodel "dorisbot/models/public"
"dorisbot/pkg/cmdparser"
@ -48,7 +49,58 @@ 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) {
func getRegServerCommandAction(s *discordgo.Session, msgEvt *discordgo.MessageCreate, server *dsmodel.DiscordServerModel, 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)
@ -59,7 +111,7 @@ func getNotInitServerCommandAction(s discordgo.Session, msgEvt *discordgo.Messag
log.Println(s)
return nil, nil, errors.New(s)
}
c := cmds[0]
c = cmds[0]
if c.RequireInit {
s := fmt.Sprintf("{%s} require init server", cmd)
log.Println(s)
@ -67,19 +119,19 @@ func getNotInitServerCommandAction(s discordgo.Session, msgEvt *discordgo.Messag
}
if c.RequireManage {
ginfo, err := s.Guild(guildID)
ginfo, err := s.Guild(msgEvt.GuildID)
if err != nil {
log.Println("get guild info err :: ", err)
return nil, nil, err
}
if ginfo.OwnerID != uid {
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(cmdmodel.Value)
acts = cmdparser.ParseCmdAction(c.Value)
return c, acts, nil
}
@ -97,9 +149,11 @@ func parseTextMessage(msg string) (cmd string, keyword bool, payload string) {
return
}
func selectAction(key, value, payload string) {
func selectAction(info EvtInfo, key, value, payload string) string {
switch key {
case "ds_addserver":
break
return addServer(info, payload)
}
return ""
}