99 lines
3.1 KiB
Go
99 lines
3.1 KiB
Go
package cfb
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"discord-cfb-bot/config"
|
|
)
|
|
|
|
type Game struct {
|
|
Game struct {
|
|
GameID string `json:"gameID"`
|
|
StartDate string `json:"startDate"`
|
|
StartTime string `json:"startTime"`
|
|
GameState string `json:"gameState"`
|
|
CurrentPeriod string `json:"currentPeriod"`
|
|
ContestClock string `json:"contestClock"`
|
|
Home Team `json:"home"`
|
|
Away Team `json:"away"`
|
|
FinalMessage string `json:"finalMessage"`
|
|
} `json:"game"`
|
|
}
|
|
|
|
type Team struct {
|
|
Score string `json:"score"`
|
|
Names struct {
|
|
Short string `json:"short"`
|
|
Full string `json:"full"`
|
|
} `json:"names"`
|
|
}
|
|
|
|
func GetGameInfo(teamName string) string {
|
|
// Make the teamName input lowercase
|
|
teamNameLower := strings.ToLower(teamName)
|
|
|
|
// Check if the lowercase teamName matches a custom abbreviation
|
|
for key, value := range config.CustomTeamNames {
|
|
if strings.ToLower(key) == teamNameLower {
|
|
teamNameLower = strings.ToLower(value)
|
|
break
|
|
}
|
|
}
|
|
|
|
apiURL := "https://ncaa.ewnix.net/scoreboard/football/fbs/2024/all-conf"
|
|
resp, err := http.Get(apiURL)
|
|
if err != nil {
|
|
return fmt.Sprintf("Failed to reach the scoreboard API: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Sprintf("Failed to reach the scoreboard API. Status code: %d", resp.StatusCode)
|
|
}
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
var apiResponse struct {
|
|
Games []Game `json:"games"`
|
|
}
|
|
if err := json.Unmarshal(body, &apiResponse); err != nil {
|
|
return "Error parsing the API response."
|
|
}
|
|
|
|
var results []string
|
|
|
|
for _, game := range apiResponse.Games {
|
|
// Filter games by team name (either home or away)
|
|
if strings.ToLower(game.Game.Home.Names.Short) == teamNameLower ||
|
|
strings.ToLower(game.Game.Away.Names.Short) == teamNameLower {
|
|
|
|
var gameInfo string
|
|
if game.Game.GameState == "in_progress" {
|
|
gameInfo = fmt.Sprintf("%s: **%s** %s: **%s** | Quarter: %s | Time Remaining: %s",
|
|
game.Game.Away.Names.Short, game.Game.Away.Score,
|
|
game.Game.Home.Names.Short, game.Game.Home.Score,
|
|
game.Game.CurrentPeriod, game.Game.ContestClock)
|
|
} else if game.Game.GameState == "scheduled" {
|
|
gameInfo = fmt.Sprintf("Upcoming: %s @ %s on %s at %s ET",
|
|
game.Game.Away.Names.Short, game.Game.Home.Names.Short, game.Game.StartDate, game.Game.StartTime)
|
|
} else if game.Game.GameState == "final" {
|
|
gameInfo = fmt.Sprintf("Final: %s: %s %s: %s",
|
|
game.Game.Away.Names.Short, game.Game.Away.Score,
|
|
game.Game.Home.Names.Short, game.Game.Home.Score)
|
|
}
|
|
|
|
results = append(results, gameInfo)
|
|
}
|
|
}
|
|
|
|
if len(results) > 0 {
|
|
return strings.Join(results, "\n")
|
|
}
|
|
|
|
return "No game found for the specified team."
|
|
}
|