gottdad/bot.go

100 lines
2.0 KiB
Go
Raw Normal View History

2020-06-21 19:51:33 +02:00
package main
import (
2020-06-21 19:54:07 +02:00
"encoding/json"
2020-06-21 19:51:33 +02:00
tb "gopkg.in/tucnak/telebot.v2"
)
func BotHandlers(b *tb.Bot) {
2020-06-22 11:02:04 +02:00
b.Handle("/pause", botPause)
b.Handle("/unpause", botUnpause)
2020-06-21 19:51:33 +02:00
b.Handle(tb.OnPhoto, botPhoto)
b.Handle(tb.OnChannelPost, botChannelPost)
b.Handle(tb.OnQuery, botQuery)
b.Handle(tb.OnText, botText)
b.Handle(tb.OnDocument, botDocument)
b.Start()
}
2020-06-22 11:02:04 +02:00
func botPause(m *tb.Message) {
forcePaused = true
2020-06-22 11:13:07 +02:00
if !paused {
paused = true
px := PacketAdminRCon{
Packet: Packet{PType: AdminPacketAdminRCon},
Command: "pause",
}
_, err := conn.Write(px.Bytes())
logErrorDebug(err, "botPause : conn.Write")
sendChat(-436055948, "Game paused.")
} else {
sendChat(-436055948, "Game already paused.")
2020-06-22 11:09:11 +02:00
}
2020-06-22 11:02:04 +02:00
return
}
func botUnpause(m *tb.Message) {
forcePaused = false
2020-06-22 11:09:38 +02:00
if paused && len(clients) > 1 {
2020-06-22 11:09:11 +02:00
paused = false
px := PacketAdminRCon{
Packet: Packet{PType: AdminPacketAdminRCon},
Command: "unpause",
}
_, err := conn.Write(px.Bytes())
logErrorDebug(err, "botUnpause : conn.Write")
2020-06-22 11:13:07 +02:00
sendChat(-436055948, "Game unpaused.")
} else {
sendChat(-436055948, "Game already unpaused.")
2020-06-22 11:09:11 +02:00
}
2020-06-22 11:02:04 +02:00
return
}
2020-06-21 19:51:33 +02:00
func PrintText(m *tb.Message) {
logInfoDebug("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
return
}
func botPhoto(m *tb.Message) {
logInfoDebug("botPhoto :", m.Text)
// photos only
}
func botDocument(m *tb.Message) {
logInfoDebug("botDocument : %s (%d bytes)\n", m.Document.FileName, m.Document.File.FileSize)
// documents only
}
func botChannelPost(m *tb.Message) {
PrintText(m)
b, _ := json.Marshal(m)
2020-06-21 19:54:07 +02:00
logInfoDebug("botChannelPost : %s\n", string(b))
2020-06-21 19:51:33 +02:00
// channel posts only
}
func botQuery(q *tb.Query) {
logInfoDebug("botQuery")
// incoming inline queries
}
func botText(m *tb.Message) {
PrintText(m)
}
2020-06-21 20:07:37 +02:00
func sendChat(chatID int64, text string) {
opt := tb.SendOptions{
ParseMode: tb.ModeDefault,
}
ch := tb.Chat{
ID: chatID,
}
2020-06-21 20:08:14 +02:00
_, err := bot.Send(&ch, text, &opt)
2020-06-21 20:08:45 +02:00
logErrorDebug(err, "sendChat")
2020-06-21 20:07:37 +02:00
}