Skip to content

Commit

Permalink
Improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
btvoidx committed Aug 20, 2021
1 parent d6eaf8e commit 655bd66
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
14 changes: 10 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)

Expand Down Expand Up @@ -50,26 +51,30 @@ func LoadConfig() Config {
fmt.Println("Config not found. Created default.")
}

fmt.Println("Loaded config.")
defer file.Close()

jsondata, _ := ioutil.ReadAll(file)
json.Unmarshal(jsondata, &config)

if config.TShockConfig != "" {
LoadTShockTokens(config.TShockConfig, &config)
if config.TShockConfig == "" {
fmt.Println("TShockConfig is not set")
os.Exit(2)
}

LoadTShockTokens(config.TShockConfig, &config)

// Make sure RestUrl does not end with /
config.RestUrl = strings.TrimSuffix(config.RestUrl, "/")

fmt.Println("Loaded config.")
return config
}

func LoadTShockTokens(path string, config *Config) {
file, err := os.Open(path)
if err != nil {
panic(err)
fmt.Println(err.Error())
os.Exit(2)
}
defer file.Close()

Expand All @@ -84,6 +89,7 @@ func LoadTShockTokens(path string, config *Config) {
continue
}

fmt.Println("Loaded tshock token for " + strconv.Itoa(userId))
config.VKUserTokens[userId] = k
}
}
59 changes: 33 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
json, err := sjson.NewFromReader(r.Body)
if err != nil {
fmt.Fprintf(w, "500")
return
}

secret, _ := json.Get("secret").String()
Expand Down Expand Up @@ -52,54 +53,60 @@ func main() {

func HandleNewMessage(json *sjson.Json) {
text, _ := json.Get("object").Get("message").Get("text").String()
from_id, _ := json.Get("object").Get("message").Get("from_id").Int()
fromId, _ := json.Get("object").Get("message").Get("from_id").Int()

// If isn't a valid command
if !strings.HasPrefix(text, "/") {
return
}

if resttoken, ok := config.VKUserTokens[from_id]; ok {
qs := &url.Values{}
qs.Add("token", resttoken)
qs.Add("cmd", text)
resttoken, ok := config.VKUserTokens[fromId]

resp, err := requests.Get(config.RestUrl+"/v3/server/rawcmd", qs, nil)
if err != nil {
SendVKMessage("Server request failed.", from_id) // Sending back err.Error() expsoses token
return
}
if !ok {
fmt.Println("id" + strconv.Itoa(fromId) + " tried to execute " + text)
return
}

json, err := sjson.NewJson(resp.Raw().Bytes())
if err != nil {
SendVKMessage("Failed parsing server response.\n"+err.Error(), from_id)
return
}
qs := &url.Values{}
qs.Add("token", resttoken)
qs.Add("cmd", text)

resp, reqerr := requests.Get(config.RestUrl+"/v3/server/rawcmd", qs, nil)
if reqerr != nil {
SendVKMessage("Server request failed.", fromId) // Sending back err.Error() expsoses token
return
}

if response, err := json.Get("response").StringArray(); err == nil {
if len(response) > 0 {
result := strings.Join(response, "\n")
SendVKMessage(result, from_id)
} else {
SendVKMessage("Command didn't return output.", from_id)
}
json, jsonerr := sjson.NewJson(resp.Raw().Bytes())
if jsonerr != nil {
SendVKMessage("Failed parsing server response.\n"+jsonerr.Error(), fromId)
return
}

fmt.Println("id" + strconv.Itoa(from_id) + " executed " + text)
if response, err := json.Get("response").StringArray(); err == nil {
if len(response) > 0 {
result := strings.Join(response, "\n")
SendVKMessage(result, fromId)
} else {
SendVKMessage("Command didn't return output.", fromId)
}

fmt.Println("id" + strconv.Itoa(fromId) + " executed " + text)
}

}

func SendVKMessage(text string, user_id int) {
func SendVKMessage(text string, userId int) {
qs := &url.Values{}
qs.Add("message", text)
qs.Add("access_token", config.VKToken)
qs.Add("keyboard", config.VKKeyboard)
qs.Add("user_id", strconv.Itoa(user_id))
qs.Add("user_id", strconv.Itoa(userId))
qs.Add("random_id", strconv.Itoa(int(time.Now().UnixNano())))
qs.Add("v", "5.131")
resp, err := requests.Get("https://api.vk.com/method/messages.send", qs, nil)
if err != nil {
fmt.Println("Error occured when sending VK message to id " + strconv.Itoa(user_id) + ": " + text)
fmt.Println("Error occured when sending VK message to id " + strconv.Itoa(userId) + ": " + text)
fmt.Println(err.Error())
fmt.Println(resp.Text())
}
Expand Down

0 comments on commit 655bd66

Please sign in to comment.