Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/broadcaster/broadcaster.go

65 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package broadcaster
import (
"time"
"source.wtfteam.pro/i2_bot/i2_bot/lib/dbmapping"
)
func (b *Broadcaster) createBroadcastMessage(playerRaw *dbmapping.Player, messageBody string, broadcastType string) (dbmapping.Broadcast, bool) {
messageRaw := dbmapping.Broadcast{}
messageRaw.Text = messageBody
messageRaw.Status = "new"
messageRaw.BroadcastType = broadcastType
messageRaw.AuthorID = playerRaw.ID
messageRaw.CreatedAt = time.Now().UTC()
_, err := c.Db.NamedExec("INSERT INTO broadcasts VALUES(NULL, :text, :broadcast_type, :status, :author_id, :created_at)", &messageRaw)
if err != nil {
c.Log.Error(err.Error())
return messageRaw, false
}
err2 := c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE author_id=? AND text=?"), messageRaw.AuthorID, messageRaw.Text)
if err2 != nil {
c.Log.Error(err2)
return messageRaw, false
}
return messageRaw, true
}
func (b *Broadcaster) getBroadcastMessageByID(messageID int) (dbmapping.Broadcast, bool) {
messageRaw := dbmapping.Broadcast{}
err := c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE id=?"), messageID)
if err != nil {
c.Log.Error(err)
return messageRaw, false
}
return messageRaw, true
}
func (b *Broadcaster) updateBroadcastMessageStatus(messageID int, messageStatus string) (dbmapping.Broadcast, bool) {
messageRaw := dbmapping.Broadcast{}
err := c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE id=?"), messageID)
if err != nil {
c.Log.Error(err.Error())
return messageRaw, false
}
messageRaw.Status = messageStatus
_, err = c.Db.NamedExec("UPDATE broadcasts SET status=:status WHERE id=:id", &messageRaw)
if err != nil {
c.Log.Error(err.Error())
return messageRaw, false
}
err = c.Db.Get(&messageRaw, c.Db.Rebind("SELECT * FROM broadcasts WHERE author_id=? AND text=?"), messageRaw.AuthorID, messageRaw.Text)
if err != nil {
c.Log.Error(err.Error())
return messageRaw, false
}
return messageRaw, true
}