package main import ( "database/sql" "flag" "fmt" _ "github.com/go-sql-driver/mysql" "gopkg.in/gcfg.v1" tb "gopkg.in/tucnak/telebot.v2" "log" "time" ) type Config struct { Telegram struct { URL string Token string } SQL struct { Driver string Type string Address string Username string Password string Database string } Rabbit struct { User string Password string Host string Queue string } Bot struct { Admin int64 Guildname string Guild string } } var ( config = flag.String("config", "chirpnest.cfg", "config file path") initdb = flag.Bool("initdb", false, "initialize bot database") db *sql.DB b *tb.Bot cfg Config MQCWMsgQueue chan ChatWarsMessage SQLMsgIdentifyQueue chan int64 TGCmdQueue chan TGCommand MQTGCmdQueue chan TGCommand msgParsingRules map[int]MessageParsingRule clientsKeepAlive map[int64]*MQKeepAlive clientsQueue map[int64]*MQClient clientsCW map[int64]*ChatWarsClient ) 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() { log.Println("Starting Chirpnest...") // Parsing config flag.Parse() err := gcfg.ReadFileInto(&cfg, *config) failOnError(err, "Parsing config") // Connecting to DB switch cfg.SQL.Driver { case "mysql": db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true") failOnError(err, "Opening MySQL connection") defer db.Close() } // Check if any issue err = db.Ping() failOnError(err, "Checking MySQL connection") log.Println("SQL connection initialized") if *initdb { initDB() _, _ = addObjCastle(`🦌`, `Deerhorn`) _, _ = addObjCastle(`🐉`, `Dragonscale`) _, _ = addObjCastle(`🦅`, `Highnest`) _, _ = addObjCastle(`🌑`, `Moonlight`) _, _ = addObjCastle(`🥔`, `Potato`) _, _ = addObjCastle(`🦈`, `Sharkteeth`) _, _ = addObjCastle(`🐺`, `Wolfpack`) _, _ = addObjGuild(``, `No Guild`) } resetMsgParsingRules() msgParsingRules, err = loadMsgParsingRules() logOnError(err, "Message parsing rules") err = loadObjCastle() logOnError(err, "Caching castles") err = loadObjGuild() logOnError(err, "Caching guilds") err = loadObjUser() logOnError(err, "Caching user") // Registering bot b, err := tb.NewBot(tb.Settings{ Token: cfg.Telegram.Token, URL: cfg.Telegram.URL, Poller: &tb.LongPoller{Timeout: 10 * time.Second}, }) failOnError(err, "Registering bot") log.Println("Bot registered") go BotHandlers(b) u := tb.User{ ID: int(cfg.Bot.Admin), } b.Send(&u, "Bot restarted") MQCWMsgQueue = make(chan ChatWarsMessage, MQCWMsgQueueSize) SQLMsgIdentifyQueue = make(chan int64, SQLMsgIdentifyQueueSize) TGCmdQueue = make(chan TGCommand, TGCmdQueueSize) MQTGCmdQueue = make(chan TGCommand, MQTGCmdQueueSize) clientsQueue = make(map[int64]*MQClient) clientsKeepAlive = make(map[int64]*MQKeepAlive) clientsCW = make(map[int64]*ChatWarsClient) for w := 1; w <= MQGetMsgWorkers; w++ { go MQGetMsgWorker(w, MQCWMsgQueue) } for w := 1; w <= SQLCWMsgWorkers; w++ { go SQLCWMsgWorker(w, MQCWMsgQueue, SQLMsgIdentifyQueue) } for w := 1; w <= SQLIdentifyMsgWorkers; w++ { go SQLIdentifyMsgWorker(w, SQLMsgIdentifyQueue) } for w := 1; w <= SQLJobWorkers; w++ { go SQLJobWorker(w) } for w := 1; w <= TGCmdWorkers; w++ { go TGCmdWorker(w, b, TGCmdQueue) } for w := 1; w <= MQTGCmdWorkers; w++ { go MQTGCmdWorker(w, MQTGCmdQueue) } go MQKeepAliveWorker() go MQTidyKeepAliveWorker() log.Println("Bot started !") // Main loop for { time.Sleep(1 * time.Second) } }