add commands model

This commit is contained in:
Jay 2019-06-30 15:05:54 +00:00
parent 45c8fc4f0e
commit dd09bfda6e
5 changed files with 112 additions and 9 deletions

18
main.go
View File

@ -1,11 +1,15 @@
package main package main
import ( import (
"dorisbot/models"
discordmodel "dorisbot/models/discord"
"dorisbot/pkg/config" "dorisbot/pkg/config"
"dorisbot/pkg/options" "dorisbot/pkg/options"
"dorisbot/pkg/svc/discord" "dorisbot/pkg/svc/discord"
"fmt" "fmt"
"log" "log"
"git.trj.tw/golang/utils"
) )
func init() { func init() {
@ -41,5 +45,19 @@ func main() {
} }
defer client.Close() defer client.Close()
_, err = models.NewDB(conf.Database)
if err != nil {
log.Fatal(err)
}
list, err := discordmodel.GetDiscordServerList()
if err != nil {
log.Fatal(err)
}
log.Println("server list ::: ", list)
for _, it := range list {
log.Println(utils.ToMap(it))
}
<-cc <-cc
} }

View File

@ -0,0 +1,49 @@
package commands
import (
"dorisbot/models"
"errors"
"time"
)
// CommandsModel -
type CommandsModel struct {
CMD string `db:"cmd"`
Server string `db:"server"`
RequireManage bool `db:"require_manage"`
RequireInit bool `db:"require_init"`
Value string `db:"value"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewCommand -
func NewCommand(cmd, server, value string, requireManage, requireInit bool) (c *CommandsModel, err error) {
if len(cmd) == 0 || len(server) == 0 || len(value) == 0 {
return nil, errors.New("cmd, server or value is empty")
}
c = &CommandsModel{
CMD: cmd,
Server: server,
Value: value,
RequireInit: requireInit,
RequireManage: requireManage,
}
return
}
// Write -
func (p *CommandsModel) Write() (err error) {
db := models.GetConn()
query := `insert into "commands" ("cmd", "server", "value", "require_init", "require_manage") values
(:cmd, :server, :value, :require_init, :require_manage) returning *`
row, err := db.NamedQuery(query, p)
if err != nil {
return err
}
err = row.StructScan(p)
return
}

View File

@ -2,6 +2,7 @@ package discord
import ( import (
"dorisbot/models" "dorisbot/models"
cmdmodel "dorisbot/models/commands"
"errors" "errors"
"time" "time"
) )
@ -11,6 +12,7 @@ type DiscordServerModel struct {
ID string `db:"id"` ID string `db:"id"`
Name string `db:"name"` Name string `db:"name"`
Permission int `db:"permission"` Permission int `db:"permission"`
Owner string `db:"owner"`
Ctime time.Time `db:"ctime"` Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"` Mtime time.Time `db:"mtime"`
} }
@ -47,6 +49,17 @@ func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel,
return return
} }
// GetDiscordServerList -
func GetDiscordServerList() (list []*DiscordServerModel, err error) {
db := models.GetConn()
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord_server`
err = db.Select(&list, query)
if err != nil {
return nil, err
}
return
}
// GetDiscordServerByID - // GetDiscordServerByID -
func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) { func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
if len(id) == 0 { if len(id) == 0 {
@ -54,7 +67,7 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
} }
db := models.GetConn() db := models.GetConn()
ds = &DiscordServerModel{} ds = &DiscordServerModel{}
query := `select "id", "name", "permission", "ctime", "mtime" from discord_server where "id" = $1` query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord_server where "id" = $1`
err = db.Get(ds, query, id) err = db.Get(ds, query, id)
return return
} }
@ -62,7 +75,7 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
// Write DiscordServer data to database // Write DiscordServer data to database
func (p *DiscordServerModel) Write() error { func (p *DiscordServerModel) Write() error {
db := models.GetConn() db := models.GetConn()
query := `insert into discord_server ("id", "name", "permission") values (:id, :name, :permission) returning *` query := `insert into discord_server ("id", "name", "permission", "owner") values (:id, :name, :permission, :owner) returning *`
row, err := db.NamedQuery(query, p) row, err := db.NamedQuery(query, p)
if err != nil { if err != nil {
return err return err
@ -112,6 +125,17 @@ func (p *DiscordServerModel) GetChannel(id string) (ch *DiscordChannelModel, err
return return
} }
// GetCommandList -
func (p *DiscordServerModel) GetCommandList() (cmds []*cmdmodel.CommandsModel, err error) {
db := models.GetConn()
query := `select "cmd", "server", "value", "require_init", "require_manage", "ctime", "mtime" from "commands" where "server" = $1`
err = db.Select(&cmds, query, p.ID)
if err != nil {
return nil, err
}
return
}
// GetRoleList - // GetRoleList -
func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, err error) { func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, err error) {
db := models.GetConn() db := models.GetConn()

View File

@ -4,6 +4,7 @@ import (
"dorisbot/pkg/config" "dorisbot/pkg/config"
"errors" "errors"
"fmt" "fmt"
"log"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@ -30,6 +31,7 @@ func NewDB(conf *config.Database) (*sqlx.DB, error) {
conf.SSLCrt, conf.SSLCrt,
conf.SSLKey, conf.SSLKey,
) )
log.Println("DB Connstr :: ", connStr)
x, err = sqlx.Connect("postgres", connStr) x, err = sqlx.Connect("postgres", connStr)
if err != nil { if err != nil {

View File

@ -1,28 +1,38 @@
package discord package discord
import ( import (
dsmodel "dorisbot/models/discord"
"log" "log"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) { func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
// skip bot message
if evt.Author.ID == client.ID {
return
}
if len(evt.Content) == 0 {
return
}
guildID := evt.GuildID guildID := evt.GuildID
channelID := evt.ChannelID channelID := evt.ChannelID
uid := evt.Author.ID uid := evt.Author.ID
_ = channelID _ = channelID
_ = uid _ = uid
ginfo, err := s.GuildRoles(guildID) server, err := dsmodel.GetDiscordServerByID(guildID)
// log.Println("get user info :: ", guildID, uid, evt.Author.Username)
// uinfo, err := s.GuildMember(guildID, uid)
if err != nil { if err != nil {
log.Println(err) log.Println("get server err :: ", err)
return return
} }
// log.Println(uinfo.Roles)
for _, it := range ginfo { cmd, keyword, payload := parseTextMessage(evt.Content)
log.Println(":: role :: ", it.ID, it.Name)
_ = cmd
_ = payload
if !keyword && server == nil {
return
} }
} }