commit 12eef70fa4b40d692172b3570fdff468fe970533 Author: Gregory Rudolph Date: Fri Nov 27 13:44:26 2020 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81f2354 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +RealmBot \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3fbb342 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module git.nightmare.haus/rudi/RealmBot + +go 1.15 + +require ( + samhofi.us/x/keybase/v2 v2.0.6 +) \ No newline at end of file diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..916b9ec --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +samhofi.us/x/keybase/v2 v2.0.6 h1:gLluTcyjbwckQxSarF1ig2klL4Li7O/THdxsgo1dUvw= +samhofi.us/x/keybase/v2 v2.0.6/go.mod h1:lJivwhzMSV+WUg+XUbatszStjjFVcuLGl+xcQpqQ5GQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5e46874 --- /dev/null +++ b/main.go @@ -0,0 +1,110 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "strconv" + "strings" + + "samhofi.us/x/keybase/v2" + "samhofi.us/x/keybase/v2/types/chat1" +) + +var ( + k = keybase.NewKeybase() + namespace = "dynamic-us" + locale = "en_US" + accessToken string + realms = make(map[int]Realm) +) + +func init() { + flag.StringVar(&accessToken, "t", "", "Token to access Blizzard API") + flag.Parse() +} + +func main() { + if accessToken == "" { + return + } + chat := handleChat + handlers := keybase.Handlers{ + ChatHandler: &chat, + } + k.Run(handlers, &keybase.RunOptions{}) +} + +func handleChat(m chat1.MsgSummary) { + if !strings.HasPrefix(m.Content.Text.Body, "!realm") { + return + } + updateRealmStatus() + search := strings.Trim(strings.Replace(m.Content.Text.Body, "!realm", "", -1), " ") + r := getRealm(search) + if r.ID == 0 { + k.ReplyByConvID(m.ConvID, m.Id, fmt.Sprintf(":interrobang: %+v was not found!", search)) + return + } + if r.Up { + k.ReplyByConvID(m.ConvID, m.Id, fmt.Sprintf(":white_check_mark: %+v is up.", r.Name)) + } else { + k.ReplyByConvID(m.ConvID, m.Id, fmt.Sprintf(":x: %+v is down.", r.Name)) + } +} + +func updateRealmStatus() { + getRealmStatus(true) + getRealmStatus(false) +} +func getRealm(search string) Realm { + id, err := strconv.Atoi(search) + if err == nil { + if val, ok := realms[id]; ok { + return val + } + } + for _, v := range realms { + if strings.Contains(strings.ToLower(v.Name), strings.ToLower(search)) { + return v + } + } + return Realm{ID: 0, Name: "Not Found", Up: false} +} + +func getRealmStatus(up bool) { + var pageData RealmSearch + statusType := "UP" + if !up { + statusType = "DOWN" + } + page := 1 + for { + // Make HTTP GET request + response, err := http.Get( + fmt.Sprintf("https://us.api.blizzard.com/data/wow/search/connected-realm?namespace=%+v&status.type=%+v&orderby=id&_page=%+v&access_token=%+v", + namespace, statusType, page, accessToken)) + if err != nil { + log.Fatal(err) + } + defer response.Body.Close() + body, readErr := ioutil.ReadAll(response.Body) + if readErr != nil { + log.Fatal(readErr) + } + err = json.Unmarshal([]byte(body), &pageData) + for _, d := range pageData.Results { + for _, r := range d.Data.Realms { + realms[r.ID] = Realm{ID: r.ID, Name: r.Name.EnUS, Up: up} + } + } + if page == pageData.PageCount || pageData.PageCount == 0 { + return + } + page++ + } + +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..bc6cc70 --- /dev/null +++ b/types.go @@ -0,0 +1,128 @@ +package main + +// RealmSearch struct is generated from https://us.api.blizzard.com/data/wow/connected-realm/index +type RealmSearch struct { + Page int `json:"page"` + PageSize int `json:"pageSize"` + MaxPageSize int `json:"maxPageSize"` + PageCount int `json:"pageCount"` + Results []struct { + Key struct { + Href string `json:"href"` + } `json:"key"` + Data struct { + Realms []struct { + IsTournament bool `json:"is_tournament"` + Timezone string `json:"timezone"` + Name struct { + ItIT string `json:"it_IT"` + RuRU string `json:"ru_RU"` + EnGB string `json:"en_GB"` + ZhTW string `json:"zh_TW"` + KoKR string `json:"ko_KR"` + EnUS string `json:"en_US"` + EsMX string `json:"es_MX"` + PtBR string `json:"pt_BR"` + EsES string `json:"es_ES"` + ZhCN string `json:"zh_CN"` + FrFR string `json:"fr_FR"` + DeDE string `json:"de_DE"` + } `json:"name"` + ID int `json:"id"` + Region struct { + Name struct { + ItIT string `json:"it_IT"` + RuRU string `json:"ru_RU"` + EnGB string `json:"en_GB"` + ZhTW string `json:"zh_TW"` + KoKR string `json:"ko_KR"` + EnUS string `json:"en_US"` + EsMX string `json:"es_MX"` + PtBR string `json:"pt_BR"` + EsES string `json:"es_ES"` + ZhCN string `json:"zh_CN"` + FrFR string `json:"fr_FR"` + DeDE string `json:"de_DE"` + } `json:"name"` + ID int `json:"id"` + } `json:"region"` + Category struct { + ItIT string `json:"it_IT"` + RuRU string `json:"ru_RU"` + EnGB string `json:"en_GB"` + ZhTW string `json:"zh_TW"` + KoKR string `json:"ko_KR"` + EnUS string `json:"en_US"` + EsMX string `json:"es_MX"` + PtBR string `json:"pt_BR"` + EsES string `json:"es_ES"` + ZhCN string `json:"zh_CN"` + FrFR string `json:"fr_FR"` + DeDE string `json:"de_DE"` + } `json:"category"` + Locale string `json:"locale"` + Type struct { + Name struct { + ItIT string `json:"it_IT"` + RuRU string `json:"ru_RU"` + EnGB string `json:"en_GB"` + ZhTW string `json:"zh_TW"` + KoKR string `json:"ko_KR"` + EnUS string `json:"en_US"` + EsMX string `json:"es_MX"` + PtBR string `json:"pt_BR"` + EsES string `json:"es_ES"` + ZhCN string `json:"zh_CN"` + FrFR string `json:"fr_FR"` + DeDE string `json:"de_DE"` + } `json:"name"` + Type string `json:"type"` + } `json:"type"` + Slug string `json:"slug"` + } `json:"realms"` + ID int `json:"id"` + HasQueue bool `json:"has_queue"` + Status struct { + Name struct { + ItIT string `json:"it_IT"` + RuRU string `json:"ru_RU"` + EnGB string `json:"en_GB"` + ZhTW string `json:"zh_TW"` + KoKR string `json:"ko_KR"` + EnUS string `json:"en_US"` + EsMX string `json:"es_MX"` + PtBR string `json:"pt_BR"` + EsES string `json:"es_ES"` + ZhCN string `json:"zh_CN"` + FrFR string `json:"fr_FR"` + DeDE string `json:"de_DE"` + } `json:"name"` + Type string `json:"type"` + } `json:"status"` + Population struct { + Name struct { + ItIT string `json:"it_IT"` + RuRU string `json:"ru_RU"` + EnGB string `json:"en_GB"` + ZhTW string `json:"zh_TW"` + KoKR string `json:"ko_KR"` + EnUS string `json:"en_US"` + EsMX string `json:"es_MX"` + PtBR string `json:"pt_BR"` + EsES string `json:"es_ES"` + ZhCN string `json:"zh_CN"` + FrFR string `json:"fr_FR"` + DeDE string `json:"de_DE"` + } `json:"name"` + Type string `json:"type"` + } `json:"population"` + } `json:"data"` + } `json:"results"` +} + +// Realm is a parsed realm from the API +type Realm struct { + ID int + Name string + Up bool +} \ No newline at end of file