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 port: 10230
database: database:
dbname: '' dbname: dorisbot
host: localhost host: localhost
port: 26257 port: 5432
user: root user: postgres
pass: '' pass: ''
ssl_mode: require
ssl_ca: './ca.crt'
ssl_crt: './client.crt'
ssl_key: './client.key'
discord: discord:
bot_token: '' bot_token: ''

View File

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

View File

@ -20,16 +20,12 @@ var x *sqlx.DB
// NewDB - // NewDB -
func NewDB(conf *config.Database) (*sqlx.DB, error) { func NewDB(conf *config.Database) (*sqlx.DB, error) {
var err 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.DBName,
conf.User, conf.User,
conf.Pass, conf.Pass,
conf.Host, conf.Host,
conf.Port, conf.Port,
conf.SSLMode,
conf.SSLCa,
conf.SSLCrt,
conf.SSLKey,
) )
log.Println("DB Connstr :: ", connStr) log.Println("DB Connstr :: ", connStr)

View File

@ -12,15 +12,11 @@ import (
// Database struct // Database struct
type Database struct { type Database struct {
DBName string `yaml:"dbname"` DBName string `yaml:"dbname"`
Host string `yaml:"host"` Host string `yaml:"host"`
Port int `yaml:"port"` Port int `yaml:"port"`
User string `yaml:"user"` User string `yaml:"user"`
Pass string `yaml:"pass"` 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 // Discord struct
@ -76,17 +72,5 @@ func LoadConfig(p ...string) (err error) {
return err 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 return
} }

View File

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