-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (72 loc) · 2.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"log"
"os"
"strconv"
"strings"
"time"
"github.com/thedadams/telegram-bot-api"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
bot, err := NewCAHBot(os.Getenv("TOKEN"))
if err != nil {
log.Panic(err)
}
defer bot.DBConn.Close()
// Remove when deployed
// bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
c := time.Tick(60 * time.Minute)
go func() {
for _ = range c {
log.Printf("Cleaning up old games.")
tx, err := bot.DBConn.Begin()
if err != nil {
log.Printf("ERROR: %v", err)
log.Printf("Failed to clean up old games.")
tx.Rollback()
continue
}
rows, err := tx.Query("SELECT clean_up_old_games()")
if err != nil {
log.Printf("ERROR: %v", err)
log.Printf("Failed to clean up old games.")
tx.Rollback()
rows.Close()
continue
}
for rows.Next() {
var GameID string
var UserID int64
if err := rows.Scan(&GameID); err != nil {
log.Printf("ERROR: %v", err)
log.Printf("Failed to clean up old games with id %v.", GameID)
continue
} else {
GameID = strings.Replace(strings.Replace(GameID, "(", "", 1), ")", "", 1)
UserID, _ = strconv.ParseInt(strings.Split(GameID, ",")[1], 10, 64)
GameID = strings.Split(GameID, ",")[0]
log.Printf("Game with id %v deleted. Let user with id %v know about it.", GameID, UserID)
bot.Send(tgbotapi.NewMessage(UserID, "Your game has been deleted because of inactivity."))
}
}
if err := rows.Err(); err != nil {
log.Printf("ERROR: %v", err)
}
rows.Close()
tx.Commit()
}
}()
for update := range updates {
messageType := bot.DetectKindMessageReceived(update)
if messageType == "callback" {
go bot.HandleUpdate(update.CallbackQuery.From, update.CallbackQuery.Message, update.CallbackQuery, messageType)
} else {
go bot.HandleUpdate(update.Message.From, update.Message, update.CallbackQuery, messageType)
}
}
}