Import from github and add cbb support
This commit is contained in:
parent
b29f567574
commit
74f4a89235
@ -2,7 +2,8 @@ package bot
|
||||
|
||||
import (
|
||||
"discord-cfb-bot/config"
|
||||
"discord-cfb-bot/internal/clients"
|
||||
cfbClient "git.ewnix.net/phlux/discord-cfb-bot/internal/clients/cfb"
|
||||
cbbClient "git.ewnix.net/discord-cfb-bot/internal/clients/cbb"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"fmt"
|
||||
"strings"
|
||||
@ -35,7 +36,13 @@ func commandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
if strings.HasPrefix(m.Content, "!cfb ") {
|
||||
teamName := strings.TrimSpace(strings.TrimPrefix(m.Content, "!cfb "))
|
||||
response := cfb.GetGameInfo(teamName)
|
||||
response := cfbClient.GetGameInfo(teamName)
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, "!cbb ") {
|
||||
teamName := strings.TrimSpace(strings.TrimPrefix(m.Content, "!cbb "))
|
||||
response := cbbClient.GetGameInfo(teamName)
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
}
|
||||
}
|
||||
|
98
internal/clients/cbb.go
Normal file
98
internal/clients/cbb.go
Normal file
@ -0,0 +1,98 @@
|
||||
package cbb
|
||||
|
||||
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/basketball-men/d1"
|
||||
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** | Half: %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."
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user