package main import ( "encoding/json" "fmt" "log" "os" "regexp" "strconv" "time" tb "gopkg.in/tucnak/telebot.v2" ) func BotHandlers(b *tb.Bot) { b.Handle("/test", botTest) b.Handle("/test_delay", botTestDelay) b.Handle("/test_html", botTestHTML) b.Handle("/test_user", botTestUser) b.Handle("/msg_rescan", botMsgRescan) b.Handle("/msg_rescan_all", botMsgRescanAll) b.Handle("/msg_dump", botMsgDump) b.Handle("/parse_rules", botListParsingRules) b.Handle("/parse_rule", botListParsingRule) b.Handle("/timer", botTimer) b.Handle("/g_deposit_all", botGDepositAll) b.Handle("/g_withdraw", botGWithdraw) b.Handle("/save_res", botSaveRes) b.Handle("/backup_export", botBackupExport) b.Handle("/backup_import", botBackupImport) b.Handle("/help", botHelp) b.Handle("/get_item_id", botGetItemId) b.Handle("/clients", botGetClients) b.Handle("/vault", botVaultHelp) b.Handle("/vault_all", botVaultAll) b.Handle("/vault_res", botVaultRes) b.Handle("/vault_alch", botVaultAlch) b.Handle("/vault_misc", botVaultMisc) b.Handle("/vault_rec", botVaultRec) b.Handle("/vault_part", botVaultPart) b.Handle("/vault_other", botVaultOther) b.Handle("/vault_item", botVaultItem) 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.Start() } func PrintText(m *tb.Message) { log.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 botPhoto(m *tb.Message) { fmt.Println("botPhoto :", m.Text) // photos only } func botDocument(m *tb.Message) { fmt.Printf("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) log.Printf("botChannelPost : %s\n", string(b)) // channel posts only } func botQuery(q *tb.Query) { fmt.Println("botQuery") // incoming inline queries } func botText(m *tb.Message) { PrintText(m) if m.Chat.ID == cfg.Bot.Depositchat && m.IsForwarded() && m.OriginalSender != nil && int64(m.OriginalSender.ID) == chtwrsbotID64 { cwm := ChatWarsMessage{ TGUserID64: int64(bot.Me.ID), TGSenderUserID64: int64(m.Sender.ID), ID64: int64(m.ID), ChatID64: int64(m.Chat.ID), Text: m.Text, Date: m.Time().UTC(), IsForwarded: true, } MQCWMsgQueue <- cwm } else if m.IsForwarded() && m.OriginalSender != nil && int64(m.OriginalSender.ID) == chtwrsbotID64 { cwm := ChatWarsMessage{ TGUserID64: int64(m.Sender.ID), TGSenderUserID64: chtwrsbotID64, ID64: int64(m.ID), ChatID64: int64(m.Chat.ID), Text: m.Text, Date: m.Time().UTC(), IsForwarded: true, } MQCWMsgQueue <- cwm } else if (m.Chat.ID == cfg.Bot.Depositchat || m.Chat.ID == cfg.Bot.Mainchat) && !m.IsForwarded() { cwm := ChatWarsMessage{ TGUserID64: int64(m.Sender.ID), TGSenderUserID64: int64(m.Sender.ID), ID64: int64(m.ID), ChatID64: int64(m.Chat.ID), Text: m.Text, Date: m.Time().UTC(), IsForwarded: false, } MQCWMsgQueue <- cwm } else if m.Private() { cwm := ChatWarsMessage{ TGUserID64: int64(m.Sender.ID), TGSenderUserID64: int64(m.Sender.ID), ID64: int64(m.ID), ChatID64: int64(m.Chat.ID), Text: m.Text, Date: m.Time().UTC(), IsForwarded: false, } MQCWMsgQueue <- cwm } // all the text messages that weren't // captured by existing handlers } func botHelp(m *tb.Message) { if !m.Private() { return } c := TGCommand{ Type: commandReplyMsg, Text: `/help - this help /msg_rescan - rescan one message /msg_rescan_all - rescan all messages /msg_dump - dump msg /parse_rules - list parsing rules\n /parse_rule - detail for one rule /timer "msg" - schedule msg for client in ETA /g_stock - check guild's vault /backup_export - export message database /backup_import - import message database from URL /get_item_id - identify item_id from string /clients - list all connected clients /g_deposit_all - deposit all res to vault /g_withdraw .. - withdraw items /vault - Deposit and withdrawal stats`, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } func botTestUser(m *tb.Message) { if !m.Private() { return } c, err := bot.ChatByID(m.Payload) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, ParseMode: cmdParseModeHTML, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: c.Username, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, ParseMode: cmdParseModeHTML, } TGCmdQueue <- c } return } func botTestHTML(m *tb.Message) { if !m.Private() { return } c := TGCommand{ Type: commandReplyMsg, Text: `bold, bold, italic, italic, inline URL, inline fixed-width code,
pre-formatted fixed-width code block
`, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, ParseMode: cmdParseModeHTML, } TGCmdQueue <- c return } func botTest(m *tb.Message) { if !m.Private() { return } if clt, ok := getLockedClient(m.Chat.ID, false); ok { clt.Mux.Unlock() clientSendCWMsg(m.Chat.ID, "🏅Me") c := TGCommand{ Type: commandReplyMsg, Text: "Test sent", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botTestDelay(m *tb.Message) { if !m.Private() { return } if clt, ok := getLockedClient(m.Chat.ID, false); ok { clt.Mux.Unlock() clientSendCWMsgDelay(m.Chat.ID, "🏅Me", 5*time.Second) c := TGCommand{ Type: commandReplyMsg, Text: "Test sent", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botGetClients(m *tb.Message) { if !m.Private() { return } if clt, ok := getLockedClient(m.Chat.ID, false); ok { clt.Mux.Unlock() muxClients.RLock() ret := fmt.Sprintf("Version %s (%s)\n", githash, buildstamp) for id, c := range clients { chat, err := bot.ChatByID(strconv.FormatInt(id, 10)) logOnError(err, "botGetClients : ChatByID") var status string if c.Active { status = `online` } else { status = `offline` } ret = fmt.Sprintf("%s%s (%s) | @%s (%s)\n", ret, c.Login, c.Build, chat.Username, status) } ret = fmt.Sprintf("%s", ret) muxClients.RUnlock() c := TGCommand{ Type: commandReplyMsg, Text: ret, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, ParseMode: cmdParseModeHTML, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botMsgRescan(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { p := JobPayloadRescanMsg{ Query: fmt.Sprintf("SELECT o.id FROM obj o WHERE o.id = %s AND o.obj_type_id = %d AND o.obj_sub_type_id = %d;", m.Payload, cacheObjType[`msg`], cacheObjSubType[`msg`]), MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) log.Printf("botMsgRescan : json : %s\n", string(b)) _, err := createJob(cacheObjSubType[`job_rescan_msg`], objJobPriorityRescanMsg, int64(m.Sender.ID), 0, time.Now().UTC(), b) logOnError(err, "botMsgRescan : createJob(cacheObjSubType[`job_rescan_msg`])") if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Error scheduling the rescan for msg #%s", m.Payload), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Rescaning msg #%s", m.Payload), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } r = regexp.MustCompile("^all$") if r.MatchString(m.Payload) { botMsgRescanAll(m) } return } func botMsgRescanAll(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } var p JobPayloadRescanMsg if len(m.Payload) > 0 { r := regexp.MustCompile("^\"(?P(.*))\"$") if r.MatchString(m.Payload) { p.Query = fmt.Sprintf("SELECT o.id FROM obj o, obj_msg om WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d AND om.obj_id = o.id AND om.text like '%s' ORDER BY o.id ASC;", cacheObjType[`msg`], cacheObjSubType[`msg`], r.ReplaceAllString(m.Payload, "${Filter}")) } else { c := TGCommand{ Type: commandReplyMsg, Text: "Wrong format", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } } else { p.Query = fmt.Sprintf("SELECT o.id FROM obj o WHERE o.obj_type_id = %d AND o.obj_sub_type_id = %d ORDER BY o.id ASC;", cacheObjType[`msg`], cacheObjSubType[`msg`]) } log.Printf("botMsgRescanAll : Query : %s\n", p.Query) p.MsgID64 = int64(m.ID) p.ChatID64 = m.Chat.ID b, _ := json.Marshal(p) _, err := createJob(cacheObjSubType[`job_rescan_msg`], objJobPriorityRescanAllMsg, int64(m.Sender.ID), 0, time.Now().UTC(), b) logOnError(err, "botMsgRescan : createJob(cacheObjSubType[`job_rescan_msg`])") if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Error scheduling the rescan for all msg.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Rescaning all msg scheduled.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botBackupExport(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } p := JobPayloadBackupExport{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) _, err := createJob(cacheObjSubType[`job_backup_export`], objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b) logOnError(err, "botBackupExport : createJob(cacheObjSubType[`job_backup_export`])") return } func botBackupImport(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } /* r := regexp.MustCompile(`^((http[s]?\:)\/\/)?([^\?\:\/#]+)(\:([0-9]+))?(\/[^\?\#]*)?(\?([^#]*))?(#.*)?.zip$`) if !r.MatchString(m.Payload) { c := TGCommand{ Type: commandReplyMsg, Text: "URL not valid.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } */ p := JobPayloadBackupImport{ URL: m.Payload, MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) _, err := createJob(cacheObjSubType[`job_backup_import`], objJobPriorityBackup, int64(m.Sender.ID), 0, time.Now().UTC(), b) logOnError(err, "botBackupImport : createJob(cacheObjSubType[`job_backup_import`])") return } func botMsgDump(m *tb.Message) { var res string if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { objId, _ := strconv.ParseInt(m.Payload, 10, 64) objTypeId, err := getObjTypeId(objId) logOnError(err, "botMsgDump : getObjSubTypeId") if err != nil { res = `Error retrieving the message` } else if objTypeId != cacheObjType[`msg`] { res = `This is not a message reference` } else { cwm, _ := getObjMsg(objId) b, _ := json.Marshal(cwm) res = string(b) } } else { res = `/msg_dump ` } c := TGCommand{ Type: commandReplyMsg, Text: res, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } func botListParsingRules(m *tb.Message) { var s string = "" if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } for _, v := range msgParsingRules { s = fmt.Sprintf("%s[%d] %s\n", s, v.ID, v.Description) if len(s) > 3000 { c := TGCommand{ Type: commandReplyMsg, Text: s, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c s = `` } } c := TGCommand{ Type: commandReplyMsg, Text: s, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } func botListParsingRule(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() if clt.TGUserID64 != cfg.Bot.Admin { c := TGCommand{ Type: commandReplyMsg, Text: "Admin only", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } r := regexp.MustCompile("^[0-9]+$") if r.MatchString(m.Payload) { i, _ := strconv.ParseInt(m.Payload, 10, 64) for _, v := range msgParsingRules { if int64(v.ID) == i { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s\n", v.Rule), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } } c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Could not find rule %s\n", m.Payload), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("/parse_rule \n"), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botGStock(m *ChatWarsMessage) { if hasUnfinishedJob(cacheObjSubType[`job_gstock`]) { c := TGCommand{ Type: commandReplyMsg, Text: "GStock is already running", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } clt, err := getLockedIdleClient() if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Busy, please retry later.", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } userID64 := clt.TGUserID64 clt.Mux.Unlock() p := JobPayloadGStock{ MsgID64: m.ID64, ChatID64: m.ChatID64, Status: 0, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) _, err = createJob(cacheObjSubType[`job_gstock`], objJobPriority, userID64, 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Stock requested", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } return } func botShops(m *ChatWarsMessage) { if hasUnfinishedJob(cacheObjSubType[`job_shops`]) { c := TGCommand{ Type: commandReplyMsg, Text: "Shops is already running", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } // fan out to all active and non idle clients clts, err := getAllIdleClientID64() if err != nil || len(clts) == 0 { c := TGCommand{ Type: commandReplyMsg, Text: "Busy, please retry later.", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } j := JobPayloadShops{ Status: 0, ChatID64: m.ChatID64, MsgID64: m.ID64, Msgs: make([]ChatWarsMessage, 0), } b, err := json.Marshal(j) logOnError(err, "botShops : Marshal") jobID64, err := createJob(cacheObjSubType[`job_shops`], objJobPriority, clts[0], 0, time.Unix(maxUnixTimestamp, 0).UTC(), b) for i, id := range clts { j2 := JobPayloadShopsSlave{ JobCallbackID64: jobID64, Status: 0, Slaves: int64(len(clts)), Shops: make([]string, 0), } for j, s := range cfg.Bot.Shops { if (j % len(clts)) == i { j2.Shops = append(j2.Shops, s.Link) } } b, err = json.Marshal(j2) logOnError(err, "botShops : Marshal Slave") _, err = createJob(cacheObjSubType[`job_shops_slave`], objJobPriority, id, 0, time.Now().UTC(), b) } c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Shops coming (ETA : %ds)", (len(cfg.Bot.Shops)/len(clts))*5), FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } func botCraftItem(m *ChatWarsMessage, r *regexp.Regexp) { if hasUnfinishedJob(cacheObjSubType[`job_craft_item`]) { c := TGCommand{ Type: commandReplyMsg, Text: "Craft Item is already running", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } clt, err := getLockedIdleClient() if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Busy, please retry later.", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } userID64 := clt.TGUserID64 clt.Mux.Unlock() p := JobPayloadCraftItem{ MsgID64: m.ID64, ChatID64: m.ChatID64, Status: 0, } p.ObjItemID64, err = getCraftItemID(r.ReplaceAllString(m.Text, "${Cmd}")) logOnError(err, "botCraftItem : getCraftItemID") if len(r.ReplaceAllString(m.Text, "${Quantity}")) > 0 { p.Quantity, err = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Quantity}"), 10, 64) logOnError(err, "botCraftItem : ParseInt") } else { p.Quantity = 1 } if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) _, err = createJob(cacheObjSubType[`job_craft_item`], objJobPriority, userID64, 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Craft summary coming", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } return } func botUserConfigPillage(m *ChatWarsMessage, set bool) { clt, _ := getLockedClient(m.TGUserID64, false) clt.Config.InterceptPillage = set clt.Mux.Unlock() botUserConfig(m) return } func botUserConfigWartime(m *ChatWarsMessage, set string) { clt, _ := getLockedClient(m.TGUserID64, false) clt.Config.Wartime = set clt.Mux.Unlock() botUserConfig(m) return } func botUserConfig(m *ChatWarsMessage) { clt, _ := getLockedClient(m.TGUserID64, false) clt.Mux.Unlock() out := fmt.Sprintf("Config for %s (running build %s):\n", clt.Login, clt.Build) if clt.Config.InterceptPillage { out = fmt.Sprintf("%s Pillage : ON (set off)\n", out) } else { out = fmt.Sprintf("%s Pillage : OFF (set on)\n", out) } if clt.Config.Wartime == `🛡Defend` { out = fmt.Sprintf("%s Wartime : DEFEND CASTLE (set to guild)\n", out) } else { out = fmt.Sprintf("%s Wartime : DEFEND GUILD (set to castle)\n", out) } c := TGCommand{ Type: commandReplyMsg, Text: out, FromMsgID64: m.ID64, FromChatID64: m.ChatID64, ParseMode: cmdParseModeHTML, } TGCmdQueue <- c return } func botAlchAll(m *ChatWarsMessage) { p := JobPayloadAlchAll{ MsgID64: m.ID64, ChatID64: m.ChatID64, Status: 0, ManaNow: 0, ManaMax: 0, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) _, err := createJob(cacheObjSubType[`job_alch_all`], objJobPriority, m.TGSenderUserID64, 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Alch all coming", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } return } func botCraftAll(m *ChatWarsMessage, r *regexp.Regexp) { if hasUnfinishedJob(cacheObjSubType[`job_craft_all`]) { c := TGCommand{ Type: commandReplyMsg, Text: "Craft All is already running", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } clt, err := getLockedIdleClient() if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Busy, please retry later.", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c return } userID64 := clt.TGUserID64 clt.Mux.Unlock() p := JobPayloadCraftAll{ MsgID64: m.ID64, ChatID64: m.ChatID64, Status: 0, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) _, err = createJob(cacheObjSubType[`job_craft_all`], objJobPriority, userID64, 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Craft all coming", FromMsgID64: m.ID64, FromChatID64: m.ChatID64, } TGCmdQueue <- c } return } func botSaveRes(m *tb.Message) { if !m.Private() { return } c := TGCommand{ Type: commandReplyMsg, Text: "Not coded yet.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } func botVaultHelp(m *tb.Message) { if !(m.Private() || m.Chat.ID == cfg.Bot.Mainchat) { return } c := TGCommand{ Type: commandReplyMsg, Text: ` /vault_all - All items /vault_res - Resources /vault_alch - Alchemy /vault_misc - Miscellaneous /vault_rec - Recipes /vault_part - Parts /vault_other - Other /vault_item ... - Specific items`, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } func botVaultAll(m *tb.Message) { l := []int64{cacheObjSubType[`item_res`], cacheObjSubType[`item_alch`], cacheObjSubType[`item_misc`], cacheObjSubType[`item_recipe`], cacheObjSubType[`item_part`], cacheObjSubType[`item_other`]} botVault(m, l) } func botVaultRes(m *tb.Message) { l := []int64{cacheObjSubType[`item_res`]} botVault(m, l) } func botVaultAlch(m *tb.Message) { l := []int64{cacheObjSubType[`item_alch`]} botVault(m, l) } func botVaultMisc(m *tb.Message) { l := []int64{cacheObjSubType[`item_misc`]} botVault(m, l) } func botVaultRec(m *tb.Message) { l := []int64{cacheObjSubType[`item_recipe`]} botVault(m, l) } func botVaultPart(m *tb.Message) { l := []int64{cacheObjSubType[`item_part`]} botVault(m, l) } func botVaultOther(m *tb.Message) { l := []int64{cacheObjSubType[`item_other`]} botVault(m, l) } func botVaultItem(m *tb.Message) { if !(m.Private() || m.Chat.ID == cfg.Bot.Mainchat) { return } p := JobPayloadVaultItemStatus{ ItemListID64: nil, DepositChatID64: cfg.Bot.Depositchat, UserID64: int64(m.Sender.ID), } r := regexp.MustCompile("([a-z][0-9]{2}[a-e]{0,1}|([0-9]{2}))") for _, l := range r.FindAllStringSubmatch(m.Payload, -1) { item := getObjItemID(l[1], ``) if item != 0 { p.ItemListID64 = append(p.ItemListID64, item) } } if len(p.ItemListID64) > 0 { b, _ := json.Marshal(p) t := time.Now().UTC() _, err := createJob(cacheObjSubType[`job_vault_item_status`], objJobPriority, int64(m.Sender.ID), 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } else { c := TGCommand{ Type: commandReplyMsg, Text: `/vault_item ...`, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botVault(m *tb.Message, itemTypeList []int64) { /* b, _ := json.Marshal(m) log.Printf("botVault (msg) :\n%s\n", string(b)) for _, e := range m.Entities { if e.Type == tb.EntityMention { log.Printf("botVault (user) : %s\n", m.Text[e.Offset:e.Offset+e.Length]) } } */ if !(m.Private() || m.Chat.ID == cfg.Bot.Mainchat) { return } p := JobPayloadVaultUserStatus{ UserID64: int64(m.Sender.ID), UserListID64: nil, ItemTypeListID64: nil, DepositChatID64: cfg.Bot.Depositchat, } p.UserListID64 = append(p.UserListID64, int64(m.Sender.ID)) p.ItemTypeListID64 = append(p.ItemTypeListID64, itemTypeList...) b, _ := json.Marshal(p) t := time.Now().UTC() _, err := createJob(cacheObjSubType[`job_vault_user_status`], objJobPriority, int64(m.Sender.ID), 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botGDepositAll(m *tb.Message) { if !m.Private() { return } p := JobPayloadGDeposit{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, ResObjID64: nil, Status: 0, } p.ResObjID64 = append(p.ResObjID64, getObjItemID(`01`, `Thread`)) if m.Sender.ID != 480149577 { p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`)) } p.ResObjID64 = append(p.ResObjID64, getObjItemID(`03`, `Pelt`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`04`, `Bone`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`05`, `Coal`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`07`, `Powder`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`08`, `Iron Ore`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`09`, `Cloth`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`10`, `Silver Ore`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`11`, `Bauxite`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`13`, `Magic Stone`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`14`, `Wooden Shaft`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`15`, `Sapphire`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`17`, `Ruby`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`18`, `Hardener`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`19`, `Steel`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`21`, `Bone Powder`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`22`, `String`)) if m.Sender.ID != 480149577 { p.ResObjID64 = append(p.ResObjID64, getObjItemID(`23`, `Coke`)) } p.ResObjID64 = append(p.ResObjID64, getObjItemID(`24`, `Purified Powder`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`28`, `Silver mold`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`31`, `Rope`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`33`, `Metal Plate`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`34`, `Metallic Fiber`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`35`, `Crafted Leather`)) list := getSQLListID64(fmt.Sprintf("select o.id from obj o where o.obj_sub_type_id = %d;", cacheObjSubType[`item_recipe`])) p.ResObjID64 = append(p.ResObjID64, list...) list = getSQLListID64(fmt.Sprintf("select o.id from obj o where o.obj_sub_type_id = %d;", cacheObjSubType[`item_part`])) p.ResObjID64 = append(p.ResObjID64, list...) /* p.ResObjID64 = append(p.ResObjID64, getObjItemID(`39`, `Stinky Sumac`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`s01`, `📕Scroll of Rage`)) p.ResObjID64 = append(p.ResObjID64, getObjItemID(`w07`, `Rapier`)) */ b, _ := json.Marshal(p) t := time.Now().UTC() _, err := createJob(cacheObjSubType[`job_gdeposit`], objJobPriority, int64(m.Chat.ID), 0, t, b) //log.Printf("botGDepositAll : json : %s\n", string(b)) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Deposit started", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botGWithdraw(m *tb.Message) { if !m.Private() { return } clt, err := getLockedIdleClient() if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Busy, please retry later.", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } userID64 := clt.TGUserID64 clt.Mux.Unlock() r := regexp.MustCompile("^(( )*[a-z0-9]+ [0-9]+( )*)+$") if r.MatchString(m.Payload) { rx := regexp.MustCompile("(?P[a-z0-9]+) (?P[0-9]+)") p := JobPayloadGWithdraw{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, UserID64: int64(m.Sender.ID), Status: 0, Validated: false, } items := []JobPayloadGWithdrawItem{} for _, l := range rx.FindAllStringSubmatch(m.Payload, -1) { log.Printf("botGWithdraw : %s / %s / %s\n", l[0], l[1], l[2]) i := l[1] q, _ := strconv.ParseInt(l[2], 10, 64) item := JobPayloadGWithdrawItem{ Code: i, Required: q, } items = append(items, item) } p.Items = items b, _ := json.Marshal(p) t := time.Now().UTC() _, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, userID64, 0, t, b) //_, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, cfg.Bot.Admin, 0, t, b) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { log.Printf("botGWithdraw : %s\n", string(b)) } } else { c := TGCommand{ Type: commandReplyMsg, Text: "Wrong format", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botTimer(m *tb.Message) { if !m.Private() { return } clt, ok := getLockedClient(m.Chat.ID, false) if !ok { c := TGCommand{ Type: commandReplyMsg, Text: "Client not registered", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c return } clt.Mux.Unlock() r := regexp.MustCompile("^(?P([0-9]*(s|m|h))+) \"(?P(.*))\"$") if r.MatchString(m.Payload) { d, err := time.ParseDuration(r.ReplaceAllString(m.Payload, "${Duration}")) if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { p := JobPayloadMsgClient{ Text: r.ReplaceAllString(m.Payload, "${Msg}"), MsgID64: int64(m.ID), ChatID64: m.Chat.ID, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(d) objID64, err := createJob(cacheObjSubType[`job_msg_client`], objJobPriority, int64(m.Chat.ID), 0, t, b) logOnError(err, "botTimer : createJob") if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Job #%d scheduled at %s", objID64, t.Format(time.RFC850)), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } } return } func botGetItemId(m *tb.Message) { if len(m.Payload) > 0 { objItemID64 := getObjItemID(``, m.Payload) if objItemID64 != 0 { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("Identified item #%d", objItemID64), FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Can''t identify item", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } } else { c := TGCommand{ Type: commandReplyMsg, Text: "No item name to identify", FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } TGCmdQueue <- c } return } func botShutdown() { os.Exit(0) }