56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type PunishmentStats struct {
|
|
Success bool `json:"success"`
|
|
WatchdogLastMinute int `json:"watchdog_lastMinute"`
|
|
WatchdogRollingDaily int `json:"watchdog_rollingDaily"`
|
|
WatchdogTotal int64 `json:"watchdog_total"`
|
|
StaffRollingDaily int `json:"staff_rollingDaily"`
|
|
StaffTotal int64 `json:"staff_total"`
|
|
}
|
|
|
|
func fetchStats(apiKey string) (*PunishmentStats, error) {
|
|
req, err := http.NewRequest("GET", "https://api.hypixel.net/v2/punishmentstats", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("API-Key", apiKey)
|
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("bad status: %s", resp.Status)
|
|
}
|
|
|
|
var stats PunishmentStats
|
|
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
|
|
return nil, err
|
|
}
|
|
return &stats, nil
|
|
}
|
|
|
|
func main() {
|
|
_ = godotenv.Load()
|
|
key := os.Getenv("HYPIXEL_API_KEY")
|
|
stats, err := fetchStats(key)
|
|
if err != nil {
|
|
fmt.Println("error:", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("%+v\n", stats)
|
|
}
|