This commit is contained in:
stryan 2021-12-22 13:37:15 -05:00
parent a0916d8294
commit eca266f58a

48
main.go
View File

@ -11,11 +11,11 @@ import (
"time" "time"
) )
type PatchList struct { type patchList struct {
Patches []Patch `json:"patches"` Patches []patch `json:"patches"`
Success bool `json:"success"` Success bool `json:"success"`
} }
type Patch struct { type patch struct {
PatchNumber string `json:"patch_number"` PatchNumber string `json:"patch_number"`
PatchName string `json:"patch_name"` PatchName string `json:"patch_name"`
PatchTimestamp int `json:"patch_timestamp"` PatchTimestamp int `json:"patch_timestamp"`
@ -23,7 +23,7 @@ type Patch struct {
PatchWebsiteAnchor string `json:"patch_website_anchor,omitempty"` PatchWebsiteAnchor string `json:"patch_website_anchor,omitempty"`
} }
func LoadPatches(pl *PatchList, url string) { func loadPatches(pl *patchList, url string) {
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
log.Printf("Got %v, retrying in 5s", err) log.Printf("Got %v, retrying in 5s", err)
@ -46,7 +46,7 @@ func LoadPatches(pl *PatchList, url string) {
} }
} }
func Notify(patch Patch, url string) { func notify(ptch patch, url string) {
type Payload struct { type Payload struct {
Text string `json:"text"` Text string `json:"text"`
@ -55,15 +55,15 @@ func Notify(patch Patch, url string) {
AvatarURL string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
} }
var msg string var msg string
if patch.PatchNumber == "" { if ptch.PatchNumber == "" {
msg = "DotaPatchBot online" msg = "DotaPatchBot online"
} else { } else {
log.Printf("Patch %v found, notifying\n", patch.PatchNumber) log.Printf("Patch %v found, notifying\n", ptch.PatchNumber)
if patch.PatchWebsite != "" { if ptch.PatchWebsite != "" {
msg = fmt.Sprintf("Patch %v released, see website:\nhttps://www.dota2.com/%v", patch.PatchNumber, patch.PatchWebsite) msg = fmt.Sprintf("Patch %v released, see website:\nhttps://www.dota2.com/%v", ptch.PatchNumber, ptch.PatchWebsite)
} else { } else {
msg = fmt.Sprintf("Patch %v Released", patch.PatchNumber) msg = fmt.Sprintf("Patch %v Released", ptch.PatchNumber)
} }
} }
if url != "" { if url != "" {
@ -101,37 +101,37 @@ func Notify(patch Patch, url string) {
func main() { func main() {
hookPtr := flag.String("hook", "", "the webhook to notify") hookPtr := flag.String("hook", "", "the webhook to notify")
sourcePtr := flag.String("url", "https://www.dota2.com/datafeed/patchnoteslist", "url to fetch patchnotes from") sourcePtr := flag.String("url", "https://www.dota2.com/datafeed/patchnoteslist", "url to fetch patchnotes from")
patch_list := PatchList{} patchlist := patchList{}
var current_patch Patch var currentPatch patch
flag.Parse() flag.Parse()
LoadPatches(&patch_list, *sourcePtr) loadPatches(&patchlist, *sourcePtr)
if len(patch_list.Patches) <= 0 { if len(patchlist.Patches) <= 0 {
log.Fatal("error getting initial patch list") log.Fatal("error getting initial patch list")
} }
current_patch = patch_list.Patches[len(patch_list.Patches)-1] currentPatch = patchlist.Patches[len(patchlist.Patches)-1]
log.Println("Started bot, loaded patch list") log.Println("Started bot, loaded patch list")
log.Printf("Starting on patch %v", current_patch.PatchNumber) log.Printf("Starting on patch %v", currentPatch.PatchNumber)
if *hookPtr == "" { if *hookPtr == "" {
log.Println("No webhook set, not notifying") log.Println("No webhook set, not notifying")
} else { } else {
log.Printf("Notifying %v", *hookPtr) log.Printf("Notifying %v", *hookPtr)
} }
Notify(Patch{}, *hookPtr) notify(patch{}, *hookPtr)
for { for {
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
LoadPatches(&patch_list, *sourcePtr) loadPatches(&patchlist, *sourcePtr)
if len(patch_list.Patches) > 0 { if len(patchlist.Patches) > 0 {
newest_patch := patch_list.Patches[len(patch_list.Patches)-1] newestPatch := patchlist.Patches[len(patchlist.Patches)-1]
if newest_patch.PatchNumber != current_patch.PatchNumber { if newestPatch.PatchNumber != currentPatch.PatchNumber {
current_patch = newest_patch currentPatch = newestPatch
if *hookPtr != "" { if *hookPtr != "" {
Notify(current_patch, *hookPtr) notify(currentPatch, *hookPtr)
} else { } else {
log.Printf("Patch %v found, no webhook\n", current_patch.PatchNumber) log.Printf("Patch %v found, no webhook\n", currentPatch.PatchNumber)
} }
} }
} }