Fix last_star_ts bug with inconsistent JSON

This commit is contained in:
Brian Buller 2019-11-08 17:41:31 -06:00
parent cf3a52201c
commit a0127890a9
2 changed files with 13 additions and 8 deletions

View File

@ -112,9 +112,7 @@ func aocBotMain(slack *Slack) {
writeToLog(" " + time.Now().Format(time.RFC3339) + " - Received Message\n") writeToLog(" " + time.Now().Format(time.RFC3339) + " - Received Message\n")
processMessage(slack, &m) processMessage(slack, &m)
} }
} }
writeToLog("== " + time.Now().Format(time.RFC3339) + " - Bot Stopped ==\n\n")
} }
func processMessage(slack *Slack, m *Message) { func processMessage(slack *Slack, m *Message) {
@ -134,6 +132,7 @@ func processMessage(slack *Slack, m *Message) {
if u, ue := slack.getUserInfo(m.User); ue == nil { if u, ue := slack.getUserInfo(m.User); ue == nil {
saveUser(u) saveUser(u)
} }
usr, err = getUser(m.User)
} }
if m.Channel != "" { if m.Channel != "" {
@ -147,6 +146,7 @@ func processMessage(slack *Slack, m *Message) {
if c, ce := slack.getChannelInfo(m.Channel); ce == nil { if c, ce := slack.getChannelInfo(m.Channel); ce == nil {
// Save channel info // Save channel info
saveChannelInfo(c) saveChannelInfo(c)
chnl, err = getChannelInfo(m.Channel)
} else { } else {
isDirectMessage = true isDirectMessage = true
} }
@ -209,7 +209,7 @@ func processMessage(slack *Slack, m *Message) {
} }
} else { } else {
fmt.Println("--- Channel Message Received") fmt.Println("--- Channel Message Received")
fmt.Println(m.Channel, m.User, m.Text) fmt.Println(chnl.Name, usr.Name, m.Text)
if m.Channel == slackChannel { if m.Channel == slackChannel {
// In the AoC channel // In the AoC channel
if strings.HasPrefix(m.Text, "!aoc") { if strings.HasPrefix(m.Text, "!aoc") {
@ -258,7 +258,7 @@ func getAoCLeaderboard(boardId string) (*Leaderboard, error) {
content, err = ioutil.ReadFile("./cookies") content, err = ioutil.ReadFile("./cookies")
if err != nil { if err != nil {
fmt.Println("Error reading Cookies") fmt.Println("Error reading Cookies")
return leaderboard, err return nil, err
} }
line := strings.TrimSpace(string(content)) line := strings.TrimSpace(string(content))
req.Header.Add("Cookie", line) req.Header.Add("Cookie", line)
@ -266,18 +266,21 @@ func getAoCLeaderboard(boardId string) (*Leaderboard, error) {
resp, err = client.Do(req) resp, err = client.Do(req)
if err != nil { if err != nil {
fmt.Println("Error getting leaderboard") fmt.Println("Error getting leaderboard")
return leaderboard, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body) body, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
fmt.Println("Error reading response body.") fmt.Println("Error reading response body.")
return leaderboard, err return nil, err
} }
err = json.Unmarshal(body, &leaderboard) strBody := string(body)
strBody = strings.ReplaceAll(strBody, "\"last_star_ts\":0", "\"last_star_ts\":\"0\"")
err = json.Unmarshal([]byte(strBody), &leaderboard)
if err != nil { if err != nil {
fmt.Println("Error on json unmarshal") fmt.Println("Error on json unmarshal")
return leaderboard, err fmt.Println(err.Error())
return nil, err
} }
for k, mbr := range leaderboard.Members { for k, mbr := range leaderboard.Members {
mbr.LastStarTs, err = time.Parse("2006-01-02T15:04:05-0700", mbr.RawStarTs) mbr.LastStarTs, err = time.Parse("2006-01-02T15:04:05-0700", mbr.RawStarTs)

2
go.mod
View File

@ -5,3 +5,5 @@ require (
github.com/br0xen/boltease v0.0.0-20170907120147-8d9019e01b5d github.com/br0xen/boltease v0.0.0-20170907120147-8d9019e01b5d
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc
) )
go 1.13