2019-07-04 09:00:16 +00:00
|
|
|
package discord
|
|
|
|
|
2019-07-09 15:05:36 +00:00
|
|
|
import (
|
|
|
|
dsmodel "dorisbot/models/discord"
|
2019-07-22 12:54:25 +00:00
|
|
|
pubmodel "dorisbot/models/public"
|
2019-09-16 07:05:27 +00:00
|
|
|
"errors"
|
2019-07-09 15:05:36 +00:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2019-09-16 07:05:27 +00:00
|
|
|
// Errors
|
|
|
|
var (
|
|
|
|
ErrCheckServer = errors.New("check server info fail")
|
|
|
|
ErrServerExists = errors.New("server already exists")
|
|
|
|
ErrCreateServer = errors.New("create server fail")
|
|
|
|
ErrGetServerOwner = errors.New("get server owner fail")
|
|
|
|
ErrWriteServer = errors.New("write server data fail")
|
|
|
|
)
|
|
|
|
|
2019-07-09 15:05:36 +00:00
|
|
|
func addServer(info EvtInfo, name string) string {
|
2019-07-21 01:41:06 +00:00
|
|
|
exists, err := dsmodel.GetServerByID(info.Server)
|
2019-07-09 15:05:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("check server exists fail :: ", err)
|
2019-09-16 07:05:27 +00:00
|
|
|
return ErrCheckServer
|
2019-07-09 15:05:36 +00:00
|
|
|
}
|
|
|
|
if exists != nil {
|
2019-09-16 07:05:27 +00:00
|
|
|
return ErrServerExists
|
2019-07-09 15:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 01:41:06 +00:00
|
|
|
ds, err := dsmodel.NewServer(info.Server, name, 0)
|
2019-07-09 15:05:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("create dsmodel fail :: ", err)
|
|
|
|
return "create server data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("show info :: ", info)
|
|
|
|
owner, err := client.GetGuildOwner(info.Server)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("get server owner err :: ", err)
|
|
|
|
return "get server owner fail"
|
|
|
|
}
|
|
|
|
ds.Owner = owner
|
|
|
|
|
|
|
|
err = ds.Write()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("write server data fail :: ", err)
|
|
|
|
return "write server data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "add server success"
|
2019-07-04 09:00:16 +00:00
|
|
|
}
|
2019-07-13 09:44:31 +00:00
|
|
|
|
2019-08-25 11:27:00 +00:00
|
|
|
func addChannel(info EvtInfo, cmd, notify string) string {
|
|
|
|
server, err := dsmodel.GetServerByID(info.Server)
|
|
|
|
if err != nil {
|
|
|
|
return "Get server data fail"
|
|
|
|
}
|
|
|
|
_ = server
|
|
|
|
|
|
|
|
if server == nil {
|
|
|
|
return "Server not init"
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := dsmodel.NewChannel(info.Channel, info.Server, cmd == "1", notify == "1")
|
|
|
|
if err != nil {
|
|
|
|
return "init channel data fail"
|
|
|
|
}
|
|
|
|
err = ch.Write()
|
|
|
|
if err != nil {
|
|
|
|
return "write channel data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Success"
|
|
|
|
}
|
|
|
|
|
2019-07-13 09:44:31 +00:00
|
|
|
func addFacebookPage(info EvtInfo, fbid, tmpl string) string {
|
2019-08-25 03:47:34 +00:00
|
|
|
log.Printf("add facebook ::: id : %s ::: tmpl : %s \n", fbid, tmpl)
|
2019-07-22 12:54:25 +00:00
|
|
|
ch, err := dsmodel.GetChannelByID(info.Channel)
|
|
|
|
if err != nil {
|
2019-08-25 11:27:00 +00:00
|
|
|
log.Println("get channel fail", err)
|
2019-07-22 12:54:25 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var page *pubmodel.FacebookPageModel
|
2019-07-13 09:44:31 +00:00
|
|
|
|
2019-07-22 12:54:25 +00:00
|
|
|
page, err = pubmodel.GetFacebookPageByID(fbid)
|
|
|
|
if err != nil {
|
|
|
|
return "check facebook fail"
|
|
|
|
}
|
|
|
|
if page == nil {
|
|
|
|
page, err = pubmodel.NewFacebookPage(fbid)
|
|
|
|
if err != nil {
|
|
|
|
return "write data fail"
|
|
|
|
}
|
|
|
|
err = page.Write()
|
|
|
|
if err != nil {
|
|
|
|
return "write data fail"
|
|
|
|
}
|
2019-08-26 04:04:46 +00:00
|
|
|
rt, err := dsmodel.CreateFacebookRT(info.Channel, page.ID, tmpl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("create fail :: ", err)
|
|
|
|
return "create data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rt.Write()
|
|
|
|
if err != nil {
|
|
|
|
return "write data fail"
|
|
|
|
}
|
2019-07-22 12:54:25 +00:00
|
|
|
} else {
|
|
|
|
fbrt, err := ch.GetFacebookPageByID(fbid)
|
|
|
|
if err != nil {
|
2019-08-25 11:27:00 +00:00
|
|
|
log.Println("get fb rt fail :: ", err)
|
2019-07-22 12:54:25 +00:00
|
|
|
return "check data fail"
|
|
|
|
}
|
2019-08-25 11:27:00 +00:00
|
|
|
|
2019-07-22 12:54:25 +00:00
|
|
|
if fbrt != nil {
|
|
|
|
return "facebook page already linked"
|
|
|
|
}
|
2019-08-19 15:13:38 +00:00
|
|
|
|
|
|
|
rt, err := dsmodel.CreateFacebookRT(info.Channel, page.ID, tmpl)
|
|
|
|
if err != nil {
|
2019-08-25 11:27:00 +00:00
|
|
|
log.Println("create fail :: ", err)
|
2019-08-19 15:13:38 +00:00
|
|
|
return "create data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rt.Write()
|
|
|
|
if err != nil {
|
|
|
|
return "write data fail"
|
|
|
|
}
|
2019-07-22 12:54:25 +00:00
|
|
|
}
|
2019-08-19 15:13:38 +00:00
|
|
|
return "Success"
|
2019-07-13 09:44:31 +00:00
|
|
|
}
|
2019-08-20 09:29:06 +00:00
|
|
|
|
|
|
|
func delFacebookPage(info EvtInfo, fbid string) string {
|
|
|
|
ch, err := dsmodel.GetChannelByID(info.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ch.DeleteFacebookPage(fbid)
|
|
|
|
if err != nil {
|
|
|
|
return "delete facebook page fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Success"
|
|
|
|
}
|
2019-09-16 07:05:27 +00:00
|
|
|
|
|
|
|
func addInstagram(info EvtInfo, igid, tmpl string) string {
|
|
|
|
log.Printf("add instagram :: ID: %v , tmpl: %v\n", igid, tmpl)
|
|
|
|
if len(igid) == 0 || len(tmpl) == 0 {
|
|
|
|
return "id or tmpl is empty"
|
|
|
|
}
|
|
|
|
ch, err := dsmodel.GetChannelByID(info.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return "get channel data fail"
|
|
|
|
}
|
|
|
|
ig, err := pubmodel.GetInstagramByID(igid)
|
|
|
|
if err != nil {
|
|
|
|
return "get instagram data fail"
|
|
|
|
}
|
|
|
|
if ig == nil {
|
|
|
|
ig, err = pubmodel.NewInstagram(igid)
|
|
|
|
if err != nil {
|
|
|
|
return "create instagram fail"
|
|
|
|
}
|
|
|
|
err = ig.Write()
|
|
|
|
if err != nil {
|
|
|
|
return "write instagram data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, err := dsmodel.CreateInstagramRT(ch.ID, igid, tmpl)
|
|
|
|
if err != nil {
|
|
|
|
return "create channel instagram link fail"
|
|
|
|
}
|
|
|
|
if err = rt.Write(); err != nil {
|
|
|
|
return "write instagram link data fail"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
igrt, err := ch.GetInstagramByID(igid)
|
|
|
|
if err != nil {
|
|
|
|
return "get instagram rt fail"
|
|
|
|
}
|
|
|
|
if igrt != nil {
|
|
|
|
return "instagram already linked"
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, err := dsmodel.CreateInstagramRT(ch.ID, igid, tmpl)
|
|
|
|
if err != nil {
|
|
|
|
return "create channel instagram link fail"
|
|
|
|
}
|
|
|
|
if err := rt.Write(); err != nil {
|
|
|
|
return "write instagram link data fail"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "Success"
|
|
|
|
}
|
|
|
|
|
|
|
|
func delInstagram(info EvtInfo, igid string) string {
|
|
|
|
ch, err := dsmodel.GetChannelByID(info.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return "get channel data fail"
|
|
|
|
}
|
|
|
|
err = ch.DeleteInstagram(igid)
|
|
|
|
if err != nil {
|
|
|
|
return "delete instagram data fail"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Success"
|
|
|
|
}
|