67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
|
package discord
|
||
|
|
||
|
import (
|
||
|
"dorisbot/models"
|
||
|
"errors"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// ChannelModel -
|
||
|
type ChannelModel 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"`
|
||
|
}
|
||
|
|
||
|
// NewChannel -
|
||
|
func NewChannel(id, server string, enableCMD, enableNotify bool) (ch *ChannelModel, err error) {
|
||
|
if len(id) == 0 || len(server) == 0 {
|
||
|
return nil, errors.New("id or server is empty")
|
||
|
}
|
||
|
ch = &ChannelModel{
|
||
|
ID: id,
|
||
|
Server: server,
|
||
|
EnableCMD: enableCMD,
|
||
|
EnableNotify: enableNotify,
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Write -
|
||
|
func (p *ChannelModel) Write() (err error) {
|
||
|
db := models.GetConn()
|
||
|
query := `insert into "discord"."channel" (id, server, enable_cmd, enable_notify) values
|
||
|
(:id, :server, :enable_cmd, :enable_notify) returning *`
|
||
|
rows, err := db.NamedQuery(query, p)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
if rows.Next() {
|
||
|
err = rows.StructScan(p)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// SetCMDState -
|
||
|
func (p *ChannelModel) SetCMDState(state bool) (err error) {
|
||
|
if state == p.EnableCMD {
|
||
|
return nil
|
||
|
}
|
||
|
db := models.GetConn()
|
||
|
query := `update "discord"."channel" set enable_cmd = $1, mtime = now() where id = $2`
|
||
|
_, err = db.Exec(query, state, p.ID)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
p.EnableCMD = state
|
||
|
return
|
||
|
}
|