136 lines
2.8 KiB
Go
136 lines
2.8 KiB
Go
package discord
|
|
|
|
import (
|
|
dsmodel "dorisbot/models/discord"
|
|
pubmodel "dorisbot/models/public"
|
|
"log"
|
|
)
|
|
|
|
func addServer(info EvtInfo, name string) string {
|
|
exists, err := dsmodel.GetServerByID(info.Server)
|
|
if err != nil {
|
|
log.Println("check server exists fail :: ", err)
|
|
return "check server info fail"
|
|
}
|
|
if exists != nil {
|
|
return "server already setup"
|
|
}
|
|
|
|
ds, err := dsmodel.NewServer(info.Server, name, 0)
|
|
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"
|
|
}
|
|
|
|
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"
|
|
}
|
|
|
|
func addFacebookPage(info EvtInfo, fbid, tmpl string) string {
|
|
log.Printf("add facebook ::: id : %s ::: tmpl : %s \n", fbid, tmpl)
|
|
ch, err := dsmodel.GetChannelByID(info.Channel)
|
|
if err != nil {
|
|
log.Println("get channel fail", err)
|
|
return ""
|
|
}
|
|
|
|
var page *pubmodel.FacebookPageModel
|
|
|
|
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"
|
|
}
|
|
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"
|
|
}
|
|
} else {
|
|
fbrt, err := ch.GetFacebookPageByID(fbid)
|
|
if err != nil {
|
|
log.Println("get fb rt fail :: ", err)
|
|
return "check data fail"
|
|
}
|
|
|
|
if fbrt != nil {
|
|
return "facebook page already linked"
|
|
}
|
|
|
|
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"
|
|
}
|
|
}
|
|
return "Success"
|
|
}
|
|
|
|
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"
|
|
}
|