gottdad/bot.go

327 lines
8.2 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"
2021-11-06 16:33:16 +01:00
"fmt"
"regexp"
"strconv"
"time"
2020-06-21 19:54:07 +02:00
2020-06-21 19:51:33 +02:00
tb "gopkg.in/tucnak/telebot.v2"
)
2021-11-06 16:33:16 +01:00
type Bot struct {
bot *tb.Bot
Config *TelegramConfig
}
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.BotHandlers()
}
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()")
}
2020-06-21 19:51:33 +02:00
2021-11-06 16:33:16 +01:00
func (b *Bot) BotHandlers() {
2020-06-22 11:02:04 +02:00
2021-11-06 16:33:16 +01:00
b.bot.Handle("/pause", botPause)
b.bot.Handle("/unpause", botUnpause)
b.bot.Handle("/register", botRegister)
2021-11-09 06:24:42 +01:00
b.bot.Handle("/deregister", botDeregister)
2021-11-06 16:33:16 +01:00
b.bot.Handle("/delete", botDelete)
b.bot.Handle("/companies", botCompanies)
b.bot.Handle("/clients", botClients)
b.bot.Handle("/players", botPlayers)
2021-11-09 10:45:01 +01:00
b.bot.Handle("/give", botGive)
b.bot.Handle("/take", botTake)
2020-06-21 19:51:33 +02:00
2021-11-06 16:33:16 +01:00
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()
2020-06-21 19:51:33 +02:00
}
2020-06-22 11:02:04 +02:00
func botPause(m *tb.Message) {
2021-11-06 16:33:16 +01:00
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
2021-11-08 07:27:49 +01:00
if !srv.Status.Paused {
srv.Pause()
} else {
bot.SendChat(bot.Config.ChatID, "Game already paused.")
}
return
2021-11-06 16:33:16 +01:00
}
}
2020-06-22 11:13:07 +02:00
}
2021-11-06 16:33:16 +01:00
}
2021-11-08 07:27:49 +01:00
bot.SendChat(bot.Config.ChatID, "You are not playing and cannot pause the game.")
2020-06-22 11:02:04 +02:00
return
}
func botUnpause(m *tb.Message) {
2021-11-06 16:33:16 +01:00
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
2021-11-08 07:27:49 +01:00
if !srv.NeedPause() {
srv.Unpause()
} else {
bot.SendChat(bot.Config.ChatID, fmt.Sprintf("Cannot unpause : %s", srv.NeedPauseReason()))
}
return
2021-11-06 16:33:16 +01:00
}
}
2020-06-22 11:09:11 +02:00
}
2021-11-06 16:33:16 +01:00
}
2021-11-08 07:27:49 +01:00
bot.SendChat(bot.Config.ChatID, "You are not playing and cannot unpause the game.")
2021-11-06 16:33:16 +01:00
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")
2020-06-22 11:13:07 +02:00
} else {
2021-11-06 16:33:16 +01:00
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) {
2021-11-08 15:24:34 +01:00
d1 := time.Now().Sub(cfg.Game.StartDate)
days := int(time.Duration(d1.Hours()) / 24)
d2 := time.Duration(days+1)*(time.Hour)*24 - d1
str := fmt.Sprintf("Update in %s\r\n", d2)
2021-11-08 14:51:21 +01:00
2021-11-06 16:33:16 +01:00
online := ""
for _, cc := range cfg.Clients {
if cc.Online {
2021-11-09 10:45:01 +01:00
online = online + fmt.Sprintf(" - %s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n"
2021-11-06 16:33:16 +01:00
}
}
offline := ""
for _, cc := range cfg.Clients {
2021-11-08 15:50:00 +01:00
if _, ok := srv.Status.Companies[cc.CompanyID]; ok && !cc.Online {
2021-11-09 10:45:01 +01:00
offline = offline + fmt.Sprintf(" - %s (%s) : %s", cc.Username, cc.TimeLeft, srv.Status.Companies[cc.CompanyID].Name) + "\r\n"
2021-11-06 16:33:16 +01:00
}
}
2021-11-08 14:51:21 +01:00
2021-11-06 16:33:16 +01:00
if len(online) > 0 {
str = str + "Players online :\r\n" + online
}
if len(offline) > 0 {
str = str + "Players offline :\r\n" + offline
}
2021-11-08 14:51:21 +01:00
2021-11-06 16:33:16 +01:00
bot.SendChat(m.Chat.ID, str)
}
2021-11-09 10:45:01 +01:00
func botGive(m *tb.Message) {
PrintText(m)
}
func botTake(m *tb.Message) {
PrintText(m)
}
2021-11-09 06:24:42 +01:00
func botDeregister(m *tb.Message) {
cc, ok := cfg.Clients[m.Sender.ID]
if !ok {
cc = &ClientConfig{
UserID: m.Sender.ID,
Username: m.Sender.Username,
CompanyID: 255,
TimeLeft: 0,
}
cfg.Clients[m.Sender.ID] = cc
bot.SendChat(m.Chat.ID, "User isn't registered.")
return
}
if cc.CompanyID != 255 {
for coID, co := range srv.Status.Companies {
if coID == cc.CompanyID {
cc.CompanyID = 255
bot.SendChat(m.Chat.ID, fmt.Sprintf("Deregistered from %s. %s playable left.", co.Name, cc.TimeLeft))
return
}
}
logInfoAlert("botRegister : %s : no such CompanyID : %d", cc.Username, cc.CompanyID)
cc.CompanyID = 255
bot.SendChat(m.Chat.ID, fmt.Sprintf("Registered company didn't exist anymore. %s playable left.", cc.TimeLeft))
return
}
return
}
2021-11-06 16:33:16 +01:00
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,
2021-11-09 06:24:42 +01:00
TimeLeft: 0,
2021-11-06 16:33:16 +01:00
}
cfg.Clients[m.Sender.ID] = cc
2021-11-09 06:24:42 +01:00
} else {
if cc.CompanyID != 255 {
for coID, co := range srv.Status.Companies {
if coID == cc.CompanyID {
bot.SendChat(m.Chat.ID, fmt.Sprintf("Already registered %s. Please /deregister first.", co.Name))
return
}
}
logInfoAlert("botRegister : %s : no such CompanyID : %d", cc.Username, cc.CompanyID)
cc.CompanyID = 255
}
2021-11-06 16:33:16 +01:00
}
2021-11-09 06:24:42 +01:00
2021-11-06 16:33:16 +01:00
coList := make(map[uint8]struct{})
for coID, _ := range srv.Status.Companies {
coList[coID] = struct{}{}
2020-06-22 11:09:11 +02:00
}
2021-11-06 16:33:16 +01:00
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)
}
}
}
2021-11-09 06:24:42 +01:00
2021-11-06 16:33:16 +01:00
if len(coList) == 0 {
bot.SendChat(m.Chat.ID, "No company to register")
return
}
2021-11-09 06:24:42 +01:00
r := regexp.MustCompile("^\\/register( )+(?P<Company>[a-zA-Z\\.\\-0-9 ]+)$")
if r.MatchString(m.Text) {
// we have a parameter
coName := r.ReplaceAllString(m.Text, "${Company}")
for coID, co := range srv.Status.Companies {
if co.Name == coName {
for _, c := range cfg.Clients {
if c.CompanyID == coID {
bot.SendChat(m.Chat.ID, fmt.Sprintf("Company %s is already registered to @%s", coName, c.Username))
return
}
}
cc.CompanyID = coID
if cc.TimeLeft == 0 {
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, fmt.Sprintf("Can't find company %s", coName))
return
}
2021-11-06 16:33:16 +01:00
if len(coList) == 1 {
for id, _ := range coList {
cc.CompanyID = id
2021-11-09 06:24:42 +01:00
if cc.TimeLeft == 0 {
days := int(time.Now().Sub(cfg.Game.StartDate).Hours() / 24)
cc.TimeLeft = cfg.Game.StartingAllotment + cfg.Game.DailyAllotment*time.Duration(days)
}
2021-11-06 16:33:16 +01:00
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)")
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)
}