dorisbot/models/discord/discord.go

174 lines
4.4 KiB
Go
Raw Normal View History

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