add commands model
This commit is contained in:
parent
45c8fc4f0e
commit
dd09bfda6e
18
main.go
18
main.go
@ -1,11 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"dorisbot/models"
|
||||
discordmodel "dorisbot/models/discord"
|
||||
"dorisbot/pkg/config"
|
||||
"dorisbot/pkg/options"
|
||||
"dorisbot/pkg/svc/discord"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -41,5 +45,19 @@ func main() {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
49
models/commands/commands.go
Normal file
49
models/commands/commands.go
Normal 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
|
||||
}
|
@ -2,6 +2,7 @@ package discord
|
||||
|
||||
import (
|
||||
"dorisbot/models"
|
||||
cmdmodel "dorisbot/models/commands"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
@ -11,6 +12,7 @@ type DiscordServerModel struct {
|
||||
ID string `db:"id"`
|
||||
Name string `db:"name"`
|
||||
Permission int `db:"permission"`
|
||||
Owner string `db:"owner"`
|
||||
Ctime time.Time `db:"ctime"`
|
||||
Mtime time.Time `db:"mtime"`
|
||||
}
|
||||
@ -47,6 +49,17 @@ func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel,
|
||||
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 -
|
||||
func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
|
||||
if len(id) == 0 {
|
||||
@ -54,7 +67,7 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
|
||||
}
|
||||
db := models.GetConn()
|
||||
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)
|
||||
return
|
||||
}
|
||||
@ -62,7 +75,7 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
|
||||
// Write DiscordServer data to database
|
||||
func (p *DiscordServerModel) Write() error {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -112,6 +125,17 @@ func (p *DiscordServerModel) GetChannel(id string) (ch *DiscordChannelModel, err
|
||||
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 -
|
||||
func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, err error) {
|
||||
db := models.GetConn()
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"dorisbot/pkg/config"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
@ -30,6 +31,7 @@ func NewDB(conf *config.Database) (*sqlx.DB, error) {
|
||||
conf.SSLCrt,
|
||||
conf.SSLKey,
|
||||
)
|
||||
log.Println("DB Connstr :: ", connStr)
|
||||
|
||||
x, err = sqlx.Connect("postgres", connStr)
|
||||
if err != nil {
|
||||
|
@ -1,28 +1,38 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
dsmodel "dorisbot/models/discord"
|
||||
"log"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
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
|
||||
channelID := evt.ChannelID
|
||||
uid := evt.Author.ID
|
||||
_ = channelID
|
||||
_ = uid
|
||||
|
||||
ginfo, err := s.GuildRoles(guildID)
|
||||
// log.Println("get user info :: ", guildID, uid, evt.Author.Username)
|
||||
// uinfo, err := s.GuildMember(guildID, uid)
|
||||
server, err := dsmodel.GetDiscordServerByID(guildID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Println("get server err :: ", err)
|
||||
return
|
||||
}
|
||||
// log.Println(uinfo.Roles)
|
||||
for _, it := range ginfo {
|
||||
log.Println(":: role :: ", it.ID, it.Name)
|
||||
|
||||
cmd, keyword, payload := parseTextMessage(evt.Content)
|
||||
|
||||
_ = cmd
|
||||
_ = payload
|
||||
if !keyword && server == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user