-
Notifications
You must be signed in to change notification settings - Fork 1
/
owCommand.go
89 lines (85 loc) · 1.84 KB
/
owCommand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
discord "github.com/bwmarrin/discordgo"
)
func OverwatchCommand(s *discord.Session, m *discord.MessageCreate, message string) {
if m.Author.Bot {
return
}
ch := make(chan string)
usertag := strings.Replace(message, "#", "-", 1)
go getStats(usertag, ch)
rank := <-ch
if rank == "" {
s.ChannelMessageSend(m.ChannelID, "There was an error fetching your stats.")
return
}
image := <-ch
level := <-ch
s.ChannelMessageSendEmbed(m.ChannelID, &discord.MessageEmbed{
Title: message,
Description: "Level: " + level + "\nRank: " + rank,
Image: &discord.MessageEmbedImage{
URL: image,
},
})
}
func getStats(user string, ch chan string) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://owapi.net/api/v3/u/"+user+"/stats", nil)
if err != nil {
ch <- ""
return
}
req.Header.Add("Accept", "text/json")
resp, err := client.Do(req)
if err != nil {
ch <- ""
return
}
read, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
ch <- ""
return
}
var dat map[string]interface{}
json.Unmarshal([]byte(read), &dat)
if dat["eu"] == nil {
ch <- ""
return
}
region := dat["eu"].(map[string]interface{})
if region == nil {
ch <- ""
return
}
stats := region["stats"].(map[string]interface{})
if stats == nil {
ch <- ""
return
}
competitive := stats["competitive"].(map[string]interface{})
if competitive == nil {
ch <- ""
return
}
overallStats := competitive["overall_stats"].(map[string]interface{})
if overallStats == nil {
ch <- ""
return
}
rank := overallStats["comprank"]
if rank == nil {
ch <- "none"
} else {
ch <- fmt.Sprint(rank.(float64))
}
ch <- overallStats["avatar"].(string)
ch <- fmt.Sprint((overallStats["prestige"].(float64) * 100) + overallStats["level"].(float64))
}