chirpnest/main.go

112 lines
1.9 KiB
Go
Raw Normal View History

2019-04-15 13:21:16 +02:00
package main
import (
2019-05-02 12:11:27 +02:00
"database/sql"
2019-05-03 05:58:36 +02:00
"flag"
"fmt"
2019-05-02 12:11:27 +02:00
_ "github.com/go-sql-driver/mysql"
2019-05-02 12:14:07 +02:00
"gopkg.in/gcfg.v1"
2019-04-15 13:21:16 +02:00
tb "gopkg.in/tucnak/telebot.v2"
2019-05-03 05:58:36 +02:00
"log"
"time"
2019-04-15 13:21:16 +02:00
)
2019-05-02 12:11:27 +02:00
type Config struct {
Telegram struct {
2019-05-03 05:58:36 +02:00
URL string
Token string
2019-05-02 12:11:27 +02:00
}
SQL struct {
2019-05-03 05:58:36 +02:00
Driver string
Type string
Address string
Username string
Password string
Database string
2019-05-02 12:11:27 +02:00
}
Rabbit struct {
2019-05-03 05:58:36 +02:00
User string
Password string
Host string
Queue string
2019-05-02 12:11:27 +02:00
}
2019-05-03 05:00:09 +02:00
Bot struct {
2019-05-03 05:58:36 +02:00
Admin uint64
Guildname string
Guild string
2019-05-03 05:00:09 +02:00
}
2019-05-02 12:11:27 +02:00
}
2019-05-03 09:15:16 +02:00
type ChatWarsMessage struct {
2019-05-04 10:57:24 +02:00
SenderUserID64 int64 `json:"sender_user_id"`
Date int32 `json:"date"`
ID64 int64 `json:"id"`
ChatID64 int64 `json:"chat_id"`
Text string `json:"text"`
2019-05-03 09:15:16 +02:00
}
2019-05-02 12:11:27 +02:00
var (
2019-05-03 05:58:36 +02:00
config = flag.String("config", "chirpnest.cfg", "config file path")
initdb = flag.Bool("initdb", false, "initialize bot database")
2019-05-02 12:11:27 +02:00
2019-05-03 05:58:36 +02:00
db *sql.DB
2019-05-04 11:01:57 +02:00
cfg Config
MQCWMsgQueue chan ChatWarsMessage
2019-05-02 12:11:27 +02:00
)
2019-04-15 13:21:16 +02:00
func PrintText(m *tb.Message) {
fmt.Printf("[%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 main() {
2019-05-02 12:11:27 +02:00
log.Println("Starting Chirpnest...")
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Parsing config
2019-05-03 05:58:36 +02:00
flag.Parse()
2019-05-02 12:14:07 +02:00
err := gcfg.ReadFileInto(&cfg, *config)
2019-05-02 12:11:27 +02:00
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Connecting to DB
switch cfg.SQL.Driver {
2019-05-03 05:58:36 +02:00
case "mysql":
db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database)
if err != nil {
log.Fatal(err)
}
defer db.Close()
2019-05-02 12:11:27 +02:00
}
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Check if any issue
err = db.Ping()
if err != nil {
log.Fatal(err)
} else {
log.Println("SQL connection initialized")
}
2019-05-03 05:58:36 +02:00
if *initdb {
2019-05-02 12:16:05 +02:00
initDB()
}
2019-05-03 05:58:36 +02:00
2019-05-03 05:23:08 +02:00
go StartBot()
2019-05-04 11:01:57 +02:00
MQCWMsgQueue = make(chan ChatWarsMessage, 100)
for w := 1; w <= 3; w++ {
go MQGetMsg(w, MQCWMsgQueue)
}
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
fmt.Println("Started !")
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
// Main loop
for {
time.Sleep(1 * time.Second)
}
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
}