This commit is contained in:
shoopea 2019-05-03 16:00:30 +08:00
parent 488eada53b
commit b55fbeac64

207
main.go
View File

@ -1,39 +1,23 @@
package main
import (
"crypto/md5"
"encoding/base32"
"encoding/json"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"sync"
"time"
"crypto/md5"
"encoding/base32"
"encoding/json"
"math/rand"
"github.com/Arman92/go-tdlib"
"github.com/streadway/amqp"
// "github.com/go-web/tokenizer"
// "database/sql"
// _ "github.com/go-sql-driver/mysql"
// "github.com/go-web/tokenizer"
// "database/sql"
// _ "github.com/go-sql-driver/mysql"
)
type MsgDirection int
const (
Incoming MsgDirection = 1
Outgoing MsgDirection = 2
)
type ChatWarsMessage struct {
UserID64 int64 `json:"user_id"`
Direction MsgDirection `json:"direction"`
MsgText string `json:"msg"`
MsgID64 int64 `json:"msg_id"`
}
type ChatWarsWithdraw struct {
Item string `json:"item"`
Quantity int64 `json:"quantity"`
@ -65,15 +49,13 @@ var hmacKey []byte
var withdrawals = map[string]ChatWarsWithdraw{}
var TgEncoding *base32.Encoding
func main() {
// msgMutex = &sync.Mutex{}
// msgMutex = &sync.Mutex{}
tdlib.SetLogVerbosityLevel(1)
tdlib.SetFilePath("./errors.txt")
TgEncoding = base32.StdEncoding.WithPadding(base32.NoPadding)
// Create new instance of client
client := tdlib.NewClient(tdlib.Config{
APIID: "187786",
@ -131,7 +113,7 @@ func main() {
go ListenCW(client)
go Listen(client)
go ListenGuild(client)
// go ForwardOwnMsg(client)
// go ForwardOwnMsg(client)
fmt.Println("Started !")
@ -146,11 +128,11 @@ func PillageKnight(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
// viaBotUserID := updateMsg.Message.ViaBotUserID
// fmt.Println("UpdateNewMessage : chatID : ", chatID)
// fmt.Println("UpdateNewMessage : senderUserID : ", senderUserID)
// fmt.Println("UpdateNewMessage : viaBotUserID : ", viaBotUserID)
if (senderUserID == user_chtwrsbot && chatID == user_chtwrsbot) {
// viaBotUserID := updateMsg.Message.ViaBotUserID
// fmt.Println("UpdateNewMessage : chatID : ", chatID)
// fmt.Println("UpdateNewMessage : senderUserID : ", senderUserID)
// fmt.Println("UpdateNewMessage : viaBotUserID : ", viaBotUserID)
if senderUserID == user_chtwrsbot && chatID == user_chtwrsbot {
return true
}
return false
@ -159,16 +141,16 @@ func PillageKnight(c *tdlib.Client) {
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
// senderUserID := updateMsg.Message.SenderUserID
// senderUserID := updateMsg.Message.SenderUserID
mType := updateMsg.Message.Content.GetMessageContentEnum()
if mType == "messageText" {
/*
/*
user, err := c.GetUser(senderUserID)
if err != nil {
fmt.Println("Pillage:", err.Error())
continue
}
*/
*/
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
for _, line := range strings.Split(strings.TrimSuffix(txt, "\n"), "\n") {
@ -178,7 +160,7 @@ func PillageKnight(c *tdlib.Client) {
time.Sleep(1 * time.Second)
fwdMsg := ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, ownUserID64)
delay := rand.Intn(20)
fmt.Println("Delaying reply by", delay + 5, "seconds")
fmt.Println("Delaying reply by", delay+5, "seconds")
time.Sleep(time.Duration(delay) * time.Second)
c.SetOption("online", tdlib.NewOptionValueBoolean(true))
time.Sleep(5 * time.Second)
@ -203,7 +185,7 @@ func PillageKnight(c *tdlib.Client) {
time.Sleep(1 * time.Second)
DeleteMsg(c, ownUserID64, fwdMsg)
time.Sleep(7 * time.Second)
// c.SetOption("online", tdlib.NewOptionValueBoolean(false))
// c.SetOption("online", tdlib.NewOptionValueBoolean(false))
break
}
match, _ = regexp.MatchString(".*/pledge.*", line)
@ -227,7 +209,7 @@ func PillageKnight(c *tdlib.Client) {
time.Sleep(1 * time.Second)
DeleteMsg(c, ownUserID64, fwdMsg)
time.Sleep(7 * time.Second)
// c.SetOption("online", tdlib.NewOptionValueBoolean(false))
// c.SetOption("online", tdlib.NewOptionValueBoolean(false))
break
}
}
@ -241,27 +223,12 @@ func ListenCW(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == user_chtwrsbot && chatID == user_chtwrsbot) {
if senderUserID == user_chtwrsbot && chatID == user_chtwrsbot {
return true
}
return false
}
conn, _ := amqp.Dial("amqp://shoopea:UmDd5g4WRa2MzqOHsG2T@localhost:5672/chatwars")
defer conn.Close()
ch, _ := conn.Channel()
defer ch.Close()
q, _ := ch.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
@ -278,36 +245,42 @@ func ListenCW(c *tdlib.Client) {
fmt.Println(txt, "\n")
fmt.Println("===============================================================================================================")
m := ChatWarsMessage{
UserID64 : ownUserID64,
MsgText : string(txt),
Direction : Incoming,
MsgID64 : updateMsg.Message.ID,
}
b, err := json.Marshal(m)
_ = ch.Publish(
"", // exchange
q.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: []byte(b),
})
fmt.Println("recv msgID64 : ", msgID64)
if msgID64 != int64(0) {
switch msgText {
case "/g_stock_res": vault.Res = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_alch": vault.Alch = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_misc": vault.Misc = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_rec": vault.Rec = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_parts": vault.Parts = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_stock_other": vault.Other = string(txt) ; msgDisplay = false; msgDelete = true
case "/g_withdraw": time.Sleep(1 * time.Second) ; _ = ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, msgUserID64) ; msgDisplay = false; msgDelete = false
default: fmt.Println("No specific task for ", msgText) ; msgDisplay = true; msgDelete = false
case "/g_stock_res":
vault.Res = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_alch":
vault.Alch = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_misc":
vault.Misc = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_rec":
vault.Rec = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_parts":
vault.Parts = string(txt)
msgDisplay = false
msgDelete = true
case "/g_stock_other":
vault.Other = string(txt)
msgDisplay = false
msgDelete = true
case "/g_withdraw":
time.Sleep(1 * time.Second)
_ = ForwardMsg(c, updateMsg.Message.ID, updateMsg.Message.ChatID, msgUserID64)
msgDisplay = false
msgDelete = false
default:
fmt.Println("No specific task for ", msgText)
msgDisplay = true
msgDelete = false
}
time.Sleep(1 * time.Second)
if msgDelete {
@ -324,7 +297,7 @@ func ListenCW(c *tdlib.Client) {
if msgDisplay {
for _, line := range strings.Split(strings.TrimSuffix(txt, "\n"), "\n") {
// fmt.Println("Testing line:", line)
// fmt.Println("Testing line:", line)
match, _ := regexp.MatchString(".*Your result on the battlefield:.*", line)
if match {
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! REPORT SPOTTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
@ -366,7 +339,7 @@ func ListenDarkwing(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
senderUserID := updateMsg.Message.SenderUserID
if (senderUserID == user_botnestbot && chatID == chat_darkwing) {
if senderUserID == user_botnestbot && chatID == chat_darkwing {
return true
}
return false
@ -407,8 +380,8 @@ func ListenGuild(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
// senderUserID := updateMsg.Message.SenderUserID
if (chatID == chat_guild) {
// senderUserID := updateMsg.Message.SenderUserID
if chatID == chat_guild {
return true
}
return false
@ -487,15 +460,15 @@ func ListenGuild(c *tdlib.Client) {
vault.ResSize = 0
vault.ResCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Res, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.ResSize = vault.ResSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.ResSize = vault.ResSize + items[r.ReplaceAllString(item, "${Code}")].Weight*int32(size)
vault.ResCount = vault.ResCount + int32(size)
}
} else {
@ -513,15 +486,15 @@ func ListenGuild(c *tdlib.Client) {
vault.AlchSize = 0
vault.AlchCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Alch, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.AlchSize = vault.AlchSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.AlchSize = vault.AlchSize + items[r.ReplaceAllString(item, "${Code}")].Weight*int32(size)
vault.AlchCount = vault.AlchCount + int32(size)
}
} else {
@ -539,15 +512,15 @@ func ListenGuild(c *tdlib.Client) {
vault.MiscSize = 0
vault.MiscCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Misc, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.MiscSize = vault.MiscSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.MiscSize = vault.MiscSize + items[r.ReplaceAllString(item, "${Code}")].Weight*int32(size)
vault.MiscCount = vault.MiscCount + int32(size)
}
} else {
@ -565,15 +538,15 @@ func ListenGuild(c *tdlib.Client) {
vault.RecSize = 0
vault.RecCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Rec, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.RecSize = vault.RecSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.RecSize = vault.RecSize + items[r.ReplaceAllString(item, "${Code}")].Weight*int32(size)
vault.RecCount = vault.RecCount + int32(size)
}
} else {
@ -591,15 +564,15 @@ func ListenGuild(c *tdlib.Client) {
vault.PartsSize = 0
vault.PartsCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Parts, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.PartsSize = vault.PartsSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.PartsSize = vault.PartsSize + items[r.ReplaceAllString(item, "${Code}")].Weight*int32(size)
vault.PartsCount = vault.PartsCount + int32(size)
}
} else {
@ -617,15 +590,15 @@ func ListenGuild(c *tdlib.Client) {
vault.OtherSize = 0
vault.OtherCount = 0
for _, item := range strings.Split(strings.TrimSuffix(vault.Other, "\n"), "\n") {
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
// fmt.Printf("%s : %#v\n", item, r.FindStringSubmatch(item))
// fmt.Printf("%s : %#v\n", item, r.SubexpNames())
if r.FindStringSubmatch(item) != nil {
if _, ok := items[r.ReplaceAllString(item, "${Code}")]; ok {
if items[r.ReplaceAllString(item, "${Code}")].Weight == -1 {
fmt.Printf("=====================> Unknown size for item : %s (%s)\n", r.ReplaceAllString(item, "${Code}"), r.ReplaceAllString(item, "${Name}"))
} else {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.OtherSize = vault.OtherSize + items[r.ReplaceAllString(item, "${Code}")].Weight * int32(size)
vault.OtherSize = vault.OtherSize + items[r.ReplaceAllString(item, "${Code}")].Weight*int32(size)
vault.OtherCount = vault.OtherCount + int32(size)
}
} else {
@ -635,7 +608,7 @@ func ListenGuild(c *tdlib.Client) {
for k, v := range items {
if strings.Compare(v.Name, s.ReplaceAllString(itemName, "${BaseName}")) == 0 {
size, _ = strconv.Atoi(r.ReplaceAllString(item, "${Quantity}"))
vault.OtherSize = vault.OtherSize + items[k].Weight * int32(size)
vault.OtherSize = vault.OtherSize + items[k].Weight*int32(size)
vault.OtherCount = vault.OtherCount + int32(size)
fmt.Printf("=====================> Found in map : %s (%d)\n", items[k].Name, size)
}
@ -650,7 +623,7 @@ func ListenGuild(c *tdlib.Client) {
}
fmt.Printf("=====================> Size for vault.Other : %d\n", vault.OtherSize)
msgText = fmt.Sprintf("Current stock [%d/38000] :\n - Resources : %d (%d)\n - Alchemist : %d (%d)\n - Misc stuff : %d (%d)\n - Recipes : %d (%d)\n - Parts : %d (%d)\n - Other : %d (%d)\n", vault.ResSize + vault.AlchSize + vault.MiscSize + vault.RecSize + vault.PartsSize + vault.OtherSize, vault.ResSize, vault.ResCount, vault.AlchSize, vault.AlchCount, vault.MiscSize, vault.MiscCount, vault.RecSize, vault.RecCount, vault.PartsSize, vault.PartsCount, vault.OtherSize, vault.OtherCount)
msgText = fmt.Sprintf("Current stock [%d/38000] :\n - Resources : %d (%d)\n - Alchemist : %d (%d)\n - Misc stuff : %d (%d)\n - Recipes : %d (%d)\n - Parts : %d (%d)\n - Other : %d (%d)\n", vault.ResSize+vault.AlchSize+vault.MiscSize+vault.RecSize+vault.PartsSize+vault.OtherSize, vault.ResSize, vault.ResCount, vault.AlchSize, vault.AlchCount, vault.MiscSize, vault.MiscCount, vault.RecSize, vault.RecCount, vault.PartsSize, vault.PartsCount, vault.OtherSize, vault.OtherCount)
inputMsgTxt = tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
sendMsg, _ = c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
msgText = ""
@ -667,10 +640,10 @@ func ListenGuild(c *tdlib.Client) {
size, _ = strconv.Atoi(t.ReplaceAllString(line, "${Quantity}"))
fmt.Printf("Withdrawing %s / %d\n", t.ReplaceAllString(line, "${Code}"), size)
m := ChatWarsWithdraw{
Item : t.ReplaceAllString(line, "${Code}"),
Quantity : int64(size),
UserID64 : int64(senderUserID),
Time : time.Now(),
Item: t.ReplaceAllString(line, "${Code}"),
Quantity: int64(size),
UserID64: int64(senderUserID),
Time: time.Now(),
}
b, _ := json.Marshal(m)
@ -679,7 +652,7 @@ func ListenGuild(c *tdlib.Client) {
md5String := TgEncoding.EncodeToString(md5Bytes[:])
withdrawals[md5String] = m;
withdrawals[md5String] = m
msgText = fmt.Sprintf("To validate @%s withdrawal of %d x %s\nClick /withdraw_%s\n", user.Username, size, items[t.ReplaceAllString(line, "${Code}")].Name, md5String)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
@ -703,7 +676,7 @@ func ListenGuild(c *tdlib.Client) {
if _, ok := withdrawals[ref]; ok {
fmt.Printf("Withdrawal %s found\n", ref)
t := time.Now()
if t.Sub(withdrawals[ref].Time) > 300 * time.Second {
if t.Sub(withdrawals[ref].Time) > 300*time.Second {
msgText = fmt.Sprintf("Reference %s is expired.\n", ref)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msgText, nil), true, true)
c.SendMessage(chat_guild, 0, false, false, nil, inputMsgTxt)
@ -744,6 +717,7 @@ func ListenGuild(c *tdlib.Client) {
}
}
}
/*
func ForwardOwnMsg(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
@ -772,8 +746,8 @@ func Listen(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
// senderUserID := updateMsg.Message.SenderUserID
if (chatID == user_chtwrsbot || chatID == user_botnestbot || chatID == user_chtwrscastlebot || chatID == chat_darkwing || chatID == chat_guild) {
// senderUserID := updateMsg.Message.SenderUserID
if chatID == user_chtwrsbot || chatID == user_botnestbot || chatID == user_chtwrscastlebot || chatID == chat_darkwing || chatID == chat_guild {
return false
}
return true
@ -818,7 +792,7 @@ func Listen(c *tdlib.Client) {
}
}
func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) (int64) {
func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) int64 {
msgIDs := make([]int64, 1)
msgIDs[0] = msgID
msgs, _ := c.ForwardMessages(toChatID, fromChatID, msgIDs, false, false, false)
@ -836,8 +810,7 @@ func DeleteMsg(c *tdlib.Client, chatID int64, msgID int64) {
fmt.Println("Deleting message ", msgID)
}
func OwnUserID(c *tdlib.Client) (int32) {
func OwnUserID(c *tdlib.Client) int32 {
user, _ := c.GetMe()
return user.ID;
return user.ID
}