big revamp
This commit is contained in:
229
bot.go
229
bot.go
@@ -2,57 +2,209 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
)
|
||||
|
||||
func BotHandlers(b *tb.Bot) {
|
||||
type Bot struct {
|
||||
bot *tb.Bot
|
||||
Config *TelegramConfig
|
||||
}
|
||||
|
||||
b.Handle("/pause", botPause)
|
||||
b.Handle("/unpause", botUnpause)
|
||||
func (b *Bot) Start() {
|
||||
var err error
|
||||
b.bot, err = tb.NewBot(tb.Settings{
|
||||
Token: b.Config.Token,
|
||||
URL: b.Config.URL,
|
||||
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
||||
})
|
||||
failError(err, "Bot.Start() : registering bot")
|
||||
|
||||
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.BotHandlers()
|
||||
}
|
||||
|
||||
b.Start()
|
||||
func (b *Bot) SendUser(id int64, msg string) {
|
||||
u := tb.User{
|
||||
ID: int(id),
|
||||
}
|
||||
_, err := b.bot.Send(&u, msg)
|
||||
logErrorDebug(err, "Bot.SendUser()")
|
||||
}
|
||||
|
||||
func (b *Bot) SendChat(chatID int64, text string) {
|
||||
opt := tb.SendOptions{
|
||||
ParseMode: tb.ModeDefault,
|
||||
}
|
||||
|
||||
ch := tb.Chat{
|
||||
ID: chatID,
|
||||
}
|
||||
_, err := b.bot.Send(&ch, text, &opt)
|
||||
logErrorDebug(err, "Bot.SendChat()")
|
||||
}
|
||||
|
||||
func (b *Bot) BotHandlers() {
|
||||
|
||||
b.bot.Handle("/pause", botPause)
|
||||
b.bot.Handle("/unpause", botUnpause)
|
||||
b.bot.Handle("/register", botRegister)
|
||||
b.bot.Handle("/delete", botDelete)
|
||||
b.bot.Handle("/companies", botCompanies)
|
||||
b.bot.Handle("/clients", botClients)
|
||||
b.bot.Handle("/players", botPlayers)
|
||||
|
||||
b.bot.Handle(tb.OnPhoto, botPhoto)
|
||||
b.bot.Handle(tb.OnChannelPost, botChannelPost)
|
||||
b.bot.Handle(tb.OnQuery, botQuery)
|
||||
b.bot.Handle(tb.OnText, botText)
|
||||
b.bot.Handle(tb.OnDocument, botDocument)
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second)
|
||||
b.SendUser(b.Config.AdminID, fmt.Sprintf("Started (%s)", version))
|
||||
}()
|
||||
|
||||
b.bot.Start()
|
||||
}
|
||||
|
||||
func botPause(m *tb.Message) {
|
||||
forcePaused = true
|
||||
if !paused {
|
||||
paused = true
|
||||
px := PacketAdminRCon{
|
||||
Packet: Packet{PType: AdminPacketAdminRCon},
|
||||
Command: "pause",
|
||||
PrintText(m)
|
||||
for userID, cc := range cfg.Clients {
|
||||
if userID == m.Sender.ID {
|
||||
if co, ok := srv.Status.Companies[cc.CompanyID]; ok {
|
||||
if clt, ok2 := srv.Status.Clients[co.ClientID]; ok2 {
|
||||
clt.Paused = true
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err := conn.Write(px.Bytes())
|
||||
logErrorDebug(err, "botPause : conn.Write")
|
||||
sendChat(-436055948, "Game paused.")
|
||||
}
|
||||
if !srv.Status.Paused {
|
||||
srv.Pause()
|
||||
bot.SendChat(bot.Config.ChatID, "Game paused.")
|
||||
} else {
|
||||
sendChat(-436055948, "Game already paused.")
|
||||
bot.SendChat(bot.Config.ChatID, "Game already paused.")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func botUnpause(m *tb.Message) {
|
||||
forcePaused = false
|
||||
if paused && len(clients) > 1 {
|
||||
paused = false
|
||||
px := PacketAdminRCon{
|
||||
Packet: Packet{PType: AdminPacketAdminRCon},
|
||||
Command: "unpause",
|
||||
PrintText(m)
|
||||
for userID, cc := range cfg.Clients {
|
||||
if userID == m.Sender.ID {
|
||||
if co, ok := srv.Status.Companies[cc.CompanyID]; ok {
|
||||
if clt, ok2 := srv.Status.Clients[co.ClientID]; ok2 {
|
||||
clt.Paused = false
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err := conn.Write(px.Bytes())
|
||||
logErrorDebug(err, "botUnpause : conn.Write")
|
||||
sendChat(-436055948, "Game unpaused.")
|
||||
} else if len(clients) == 1 {
|
||||
sendChat(-436055948, "No players, cannot unpause.")
|
||||
} else {
|
||||
sendChat(-436055948, "Game already unpaused.")
|
||||
}
|
||||
if !srv.NeedPause() {
|
||||
srv.Unpause()
|
||||
bot.SendChat(bot.Config.ChatID, "Game unpaused.")
|
||||
} else {
|
||||
bot.SendChat(bot.Config.ChatID, fmt.Sprintf("Cannot unpause : %s", srv.NeedPauseReason()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func botDelete(m *tb.Message) {
|
||||
logInfoDebug("[%d] %s(%d) | %s(%d) : delete : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
|
||||
if m.Sender.ID == int(bot.Config.AdminID) {
|
||||
r := regexp.MustCompile("/delete (?P<CompanyID>[0-9]+)")
|
||||
ID64, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${CompanyID}"), 10, 64)
|
||||
srv.DeleteCompany(uint8(ID64))
|
||||
bot.SendChat(m.Chat.ID, "Deleting")
|
||||
} else {
|
||||
bot.SendChat(m.Chat.ID, "Not authorized to delete")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func botCompanies(m *tb.Message) {
|
||||
str := "Companies :"
|
||||
for k, v := range srv.Status.Companies {
|
||||
str = str + "\r\n" + fmt.Sprintf(" - %s (%d)", v.Name, k)
|
||||
}
|
||||
bot.SendChat(m.Chat.ID, str)
|
||||
}
|
||||
|
||||
func botClients(m *tb.Message) {
|
||||
str := "Clients :"
|
||||
for k, v := range srv.Status.Clients {
|
||||
str = str + "\r\n" + fmt.Sprintf(" - %s (%d) : company #%d", v.Name, k, v.CompanyID)
|
||||
}
|
||||
bot.SendChat(m.Chat.ID, str)
|
||||
}
|
||||
|
||||
func botPlayers(m *tb.Message) {
|
||||
online := ""
|
||||
for _, cc := range cfg.Clients {
|
||||
if cc.Online {
|
||||
online = online + fmt.Sprintf(" - @%s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n"
|
||||
}
|
||||
}
|
||||
offline := ""
|
||||
for _, cc := range cfg.Clients {
|
||||
if !cc.Online {
|
||||
offline = offline + fmt.Sprintf(" - @%s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n"
|
||||
}
|
||||
}
|
||||
str := ""
|
||||
if len(online) > 0 {
|
||||
str = str + "Players online :\r\n" + online
|
||||
}
|
||||
if len(offline) > 0 {
|
||||
str = str + "Players offline :\r\n" + offline
|
||||
}
|
||||
if str == "" {
|
||||
str = "No players."
|
||||
}
|
||||
bot.SendChat(m.Chat.ID, str)
|
||||
}
|
||||
|
||||
func botRegister(m *tb.Message) {
|
||||
cc, ok := cfg.Clients[m.Sender.ID]
|
||||
if !ok {
|
||||
cc = &ClientConfig{
|
||||
UserID: m.Sender.ID,
|
||||
Username: m.Sender.Username,
|
||||
CompanyID: 255,
|
||||
}
|
||||
cfg.Clients[m.Sender.ID] = cc
|
||||
}
|
||||
coList := make(map[uint8]struct{})
|
||||
for coID, _ := range srv.Status.Companies {
|
||||
coList[coID] = struct{}{}
|
||||
}
|
||||
for _, c := range cfg.Clients {
|
||||
if c.CompanyID != 255 {
|
||||
if _, ok := coList[c.CompanyID]; !ok {
|
||||
logInfoAlert("botRegister : %s : no such CompanyID : %d", c.Username, c.CompanyID)
|
||||
c.CompanyID = 255
|
||||
} else {
|
||||
delete(coList, c.CompanyID)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if len(coList) == 0 {
|
||||
bot.SendChat(m.Chat.ID, "No company to register")
|
||||
return
|
||||
}
|
||||
if len(coList) == 1 {
|
||||
for id, _ := range coList {
|
||||
cc.CompanyID = id
|
||||
days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24)
|
||||
cc.TimeLeft = cfg.Game.StartingAllotment + cfg.Game.DailyAllotment*time.Duration(days)
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s registered %s (with %s playable)", cc.Username, srv.Status.Companies[cc.CompanyID].Name, cc.TimeLeft))
|
||||
return
|
||||
}
|
||||
}
|
||||
bot.SendChat(m.Chat.ID, "More than one company unregistered. Wait for bot update (poke @tiennou)")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -75,7 +227,6 @@ func botChannelPost(m *tb.Message) {
|
||||
PrintText(m)
|
||||
b, _ := json.Marshal(m)
|
||||
logInfoDebug("botChannelPost : %s\n", string(b))
|
||||
|
||||
// channel posts only
|
||||
}
|
||||
|
||||
@@ -87,15 +238,3 @@ func botQuery(q *tb.Query) {
|
||||
func botText(m *tb.Message) {
|
||||
PrintText(m)
|
||||
}
|
||||
|
||||
func sendChat(chatID int64, text string) {
|
||||
opt := tb.SendOptions{
|
||||
ParseMode: tb.ModeDefault,
|
||||
}
|
||||
|
||||
ch := tb.Chat{
|
||||
ID: chatID,
|
||||
}
|
||||
_, err := bot.Send(&ch, text, &opt)
|
||||
logErrorDebug(err, "sendChat")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user