2019-06-28 15:11:58 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dorisbot/models"
|
2019-06-30 15:05:54 +00:00
|
|
|
cmdmodel "dorisbot/models/commands"
|
2019-06-28 15:11:58 +00:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DiscordServerModel -
|
|
|
|
type DiscordServerModel struct {
|
|
|
|
ID string `db:"id"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
Permission int `db:"permission"`
|
2019-06-30 15:05:54 +00:00
|
|
|
Owner string `db:"owner"`
|
2019-06-28 15:11:58 +00:00
|
|
|
Ctime time.Time `db:"ctime"`
|
|
|
|
Mtime time.Time `db:"mtime"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscordChannelModel -
|
|
|
|
type DiscordChannelModel struct {
|
|
|
|
ID string `db:"id"`
|
|
|
|
Server string `db:"server"`
|
|
|
|
EnableCMD bool `db:"enable_cmd"`
|
|
|
|
EnableNotify bool `db:"enable_notify"`
|
|
|
|
Ctime time.Time `db:"ctime"`
|
|
|
|
Mtime time.Time `db:"mtime"`
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:29:11 +00:00
|
|
|
// DiscordManageRoleModel -
|
|
|
|
type DiscordManageRoleModel 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"`
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:11:58 +00:00
|
|
|
// NewDiscordServer -
|
|
|
|
func NewDiscordServer(id, name string, permission int) (ds *DiscordServerModel, err error) {
|
|
|
|
if len(id) == 0 || len(name) == 0 {
|
|
|
|
return nil, errors.New("id or name empty")
|
|
|
|
}
|
|
|
|
ds = &DiscordServerModel{
|
|
|
|
ID: id,
|
|
|
|
Name: name,
|
|
|
|
Permission: permission,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-30 15:05:54 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:11:58 +00:00
|
|
|
// GetDiscordServerByID -
|
|
|
|
func GetDiscordServerByID(id string) (ds *DiscordServerModel, err error) {
|
|
|
|
if len(id) == 0 {
|
|
|
|
return nil, errors.New("id is empty")
|
|
|
|
}
|
|
|
|
db := models.GetConn()
|
|
|
|
ds = &DiscordServerModel{}
|
2019-06-30 15:05:54 +00:00
|
|
|
query := `select "id", "name", "permission", "owner", "ctime", "mtime" from discord_server where "id" = $1`
|
2019-06-28 15:11:58 +00:00
|
|
|
err = db.Get(ds, query, id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write DiscordServer data to database
|
|
|
|
func (p *DiscordServerModel) Write() error {
|
|
|
|
db := models.GetConn()
|
2019-06-30 15:05:54 +00:00
|
|
|
query := `insert into discord_server ("id", "name", "permission", "owner") values (:id, :name, :permission, :owner) returning *`
|
2019-06-28 15:11:58 +00:00
|
|
|
row, err := db.NamedQuery(query, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = row.StructScan(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeName - change discord server name
|
|
|
|
func (p *DiscordServerModel) 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 *DiscordServerModel) GetChannelList() (list []*DiscordChannelModel, 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 *DiscordServerModel) GetChannel(id string) (ch *DiscordChannelModel, err error) {
|
|
|
|
if len(id) == 0 {
|
|
|
|
return nil, errors.New("id is empty")
|
|
|
|
}
|
2019-06-30 05:29:11 +00:00
|
|
|
db := models.GetConn()
|
|
|
|
ch = &DiscordChannelModel{}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-30 15:05:54 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:29:11 +00:00
|
|
|
// GetRoleList -
|
|
|
|
func (p *DiscordServerModel) GetRoleList() (list []*DiscordManageRoleModel, 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 *DiscordServerModel) GetRole(roleID string) (role *DiscordManageRoleModel, err error) {
|
|
|
|
if len(roleID) == 0 {
|
|
|
|
return nil, errors.New("id is empty")
|
|
|
|
}
|
|
|
|
db := models.GetConn()
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-28 15:11:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddChannel -
|
|
|
|
func (p *DiscordServerModel) 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
|
|
|
|
}
|