dorisbot/models/discord/discord.go
2019-07-13 17:44:31 +08:00

174 lines
4.4 KiB
Go

package discord
import (
"database/sql"
"dorisbot/models"
pubmodel "dorisbot/models/public"
"errors"
"time"
)
// ServerModel -
type ServerModel 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"`
}
// ManageRoleModel -
type ManageRoleModel struct {
Server string `db:"server"`
RoleID string `db:"role_id"`
RoleName string `db:"role_name"`
Ctime time.Time `db:"ctime"`
Mtime time.Time `db:"mtime"`
}
// NewServer -
func NewServer(id, name string, permission int) (ds *ServerModel, err error) {
if len(id) == 0 || len(name) == 0 {
return nil, errors.New("id or name empty")
}
ds = &ServerModel{
ID: id,
Name: name,
Permission: permission,
}
return
}
// GetServerList -
func GetServerList() (list []*ServerModel, 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
}
// GetServerByID -
func GetServerByID(id string) (ds *ServerModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
db := models.GetConn()
ds = &ServerModel{}
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
}
// Write Server data to database
func (p *ServerModel) Write() error {
db := models.GetConn()
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
}
defer row.Close()
if row.Next() {
err = row.StructScan(p)
if err != nil {
return err
}
}
return nil
}
// ChangeName - change discord server name
func (p *ServerModel) ChangeName(name string) (err error) {
if len(name) == 0 {
return errors.New("name is empty")
}
db := models.GetConn()
query := `update discord.server set "name" = $1, "mtime" = now() where "id" = $2`
_, err = db.Exec(query, name, p.ID)
if err != nil {
return err
}
p.Name = name
return
}
// GetChannelList -
func (p *ServerModel) GetChannelList() (list []*ChannelModel, err error) {
db := models.GetConn()
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "server" = $1`
err = db.Select(&list, query, p.ID)
if err != nil {
return nil, err
}
return
}
// GetChannel -
func (p *ServerModel) GetChannel(id string) (ch *ChannelModel, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
db := models.GetConn()
ch = &ChannelModel{}
query := `select "id", "server", "enable_cmd", "enable_notify", "ctime", "mtime" from discord.channel where "id" = $1 and "server" = $2`
err = db.Get(ch, query, id, p.ID)
return
}
// GetCommandList -
func (p *ServerModel) GetCommandList() (cmds []*pubmodel.CommandModel, err error) {
db := models.GetConn()
query := `select "cmd", "server", "value", "require_init", "require_manage", "ctime", "mtime" from public."command" where "server" = $1`
err = db.Select(&cmds, query, p.ID)
if err != nil {
return nil, err
}
return
}
// GetRoleList -
func (p *ServerModel) GetRoleList() (list []*ManageRoleModel, err error) {
db := models.GetConn()
query := `select "server", "role_id", "role_name", "ctime", "mtime" from "discord"."manage_role" where "server" = $1`
err = db.Select(&list, query, p.ID)
if err != nil {
return nil, err
}
return
}
// GetRole -
func (p *ServerModel) GetRole(roleID string) (role *ManageRoleModel, err error) {
if len(roleID) == 0 {
return nil, errors.New("id is empty")
}
db := models.GetConn()
role = &ManageRoleModel{}
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 && err != sql.ErrNoRows {
return nil, err
}
return
}
// AddChannel -
func (p *ServerModel) AddChannel(id string, enableCMD, enableNotify bool) (err error) {
db := models.GetConn()
query := `insert into discord.channel ("id", "server", "enable_cmd", "enable_notify", "ctime", "mtime") values
($1, $2, $3, $4, now(), now())`
_, err = db.Exec(query, id, p.ID, enableCMD, enableNotify)
if err != nil {
return err
}
return
}