Holy shit this was hard

This commit is contained in:
2024-11-10 21:18:37 -05:00
parent 9eee8c7f9c
commit cd33fd2584
3 changed files with 119 additions and 66 deletions

View File

@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"discord-cfb-bot/config"
"unicode"
)
type Game struct {
@ -33,18 +33,24 @@ type Team struct {
}
func GetGameInfo(teamName string) string {
// Make the teamName input lowercase
teamNameLower := strings.ToLower(teamName)
teamName = strings.TrimSpace(teamName)
week := ""
// 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
}
// Check if the last two characters of the input are integers for the week
if len(teamName) >= 2 && unicode.IsDigit(rune(teamName[len(teamName)-1])) && unicode.IsDigit(rune(teamName[len(teamName)-2])) {
week = teamName[len(teamName)-2:]
teamName = strings.TrimSpace(teamName[:len(teamName)-2])
log.Printf("Week detected: %s", week)
}
apiURL := "https://ncaa.ewnix.net/scoreboard/football/fbs/2024/all-conf"
// Build the API URL based on whether a week is provided
apiURL := "https://ncaa.ewnix.net/scoreboard/football/fbs"
if week != "" {
apiURL = fmt.Sprintf("https://ncaa.ewnix.net/scoreboard/football/fbs/2024/%s/all-conf", week)
}
log.Printf("Fetching data from API: %s", apiURL)
resp, err := http.Get(apiURL)
if err != nil {
return fmt.Sprintf("Failed to reach the scoreboard API: %v", err)
@ -65,23 +71,29 @@ func GetGameInfo(teamName string) string {
}
var results []string
teamNameLower := strings.ToLower(teamName)
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 {
homeTeamLowerShort := strings.ToLower(game.Game.Home.Names.Short)
awayTeamLowerShort := strings.ToLower(game.Game.Away.Names.Short)
// Check if the team matches the short name only
if homeTeamLowerShort == teamNameLower || awayTeamLowerShort == teamNameLower {
var gameInfo string
if game.Game.GameState == "in_progress" {
if game.Game.GameState == "live" {
period := game.Game.CurrentPeriod
if period == "" {
period = "HALFTIME"
}
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" {
period, game.Game.ContestClock)
} else if game.Game.GameState == "pre" {
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",
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)
}
@ -96,3 +108,4 @@ func GetGameInfo(teamName string) string {
return "No game found for the specified team."
}