updaye database , and model

This commit is contained in:
Jay 2019-07-02 10:05:26 +00:00
parent 462804ed07
commit 30d38d6d20
5 changed files with 95 additions and 34 deletions

View File

@ -1,13 +1,9 @@
port: 10230
database:
dbname: ''
dbname: dorisbot
host: localhost
port: 26257
user: root
port: 5432
user: postgres
pass: ''
ssl_mode: require
ssl_ca: './ca.crt'
ssl_crt: './client.crt'
ssl_key: './client.key'
discord:
bot_token: ''

View File

@ -1,6 +1,7 @@
package discord
import (
"database/sql"
"dorisbot/models"
pubmodel "dorisbot/models/public"
"errors"
@ -69,6 +70,9 @@ func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
ds = &DiscordServerModel{}
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord.server where "id" = $1`
err = db.Get(ds, query, id)
if err == sql.ErrNoRows {
return nil, nil
}
return
}
@ -156,7 +160,7 @@ func (p *DiscordServerModel) GetRole(roleID string) (role *DiscordManageRoleMode
role = &DiscordManageRoleModel{}
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1 and "rold_id" = $2`
err = db.Get(role, query, p.ID, roleID)
if err != nil {
if err != nil && err != sql.ErrNoRows {
return nil, err
}
return

View File

@ -20,16 +20,12 @@ var x *sqlx.DB
// NewDB -
func NewDB(conf *config.Database) (*sqlx.DB, error) {
var err error
connStr := fmt.Sprintf("dbname=%s user=%s password=%s host=%s port=%d sslmode=%s sslrootcert=%s sslcert=%s sslkey=%s",
connStr := fmt.Sprintf("dbname=%s user=%s password=%s host=%s port=%d sslmode=disable",
conf.DBName,
conf.User,
conf.Pass,
conf.Host,
conf.Port,
conf.SSLMode,
conf.SSLCa,
conf.SSLCrt,
conf.SSLKey,
)
log.Println("DB Connstr :: ", connStr)

View File

@ -17,10 +17,6 @@ type Database struct {
Port int `yaml:"port"`
User string `yaml:"user"`
Pass string `yaml:"pass"`
SSLCa string `yaml:"ssl_ca"`
SSLCrt string `yaml:"ssl_crt"`
SSLKey string `yaml:"ssl_key"`
SSLMode string `yaml:"ssl_mode"`
}
// Discord struct
@ -76,17 +72,5 @@ func LoadConfig(p ...string) (err error) {
return err
}
if exists := utils.CheckExists(conf.Database.SSLCa, false); !exists {
return errors.New("ssl ca file not exists")
}
if exists := utils.CheckExists(conf.Database.SSLCrt, false); !exists {
return errors.New("ssl crt file not exists")
}
if exists := utils.CheckExists(conf.Database.SSLKey, false); !exists {
return errors.New("ssl key file not exists")
}
return
}

View File

@ -2,6 +2,7 @@ package discord
import (
dsmodel "dorisbot/models/discord"
pubmodel "dorisbot/models/public"
"log"
"github.com/bwmarrin/discordgo"
@ -35,6 +36,86 @@ func messageCreateEvt(s *discordgo.Session, evt *discordgo.MessageCreate) {
return
}
// 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
} 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]
// TODO: check user role group
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
}
}
// TODO: command parser
}
}
func guildMemberAddEvt(s *discordgo.Session, evt *discordgo.GuildMemberAdd) {}