Rewrite for new API

This commit is contained in:
Kevin Thompson
2024-11-10 17:23:41 -06:00
parent 801396923b
commit 856e1b1cbd
5 changed files with 113 additions and 126 deletions

View File

@ -1,41 +1,41 @@
package bot
import (
"discord-cfb-bot/config"
"discord-cfb-bot/internal/clients"
"github.com/bwmarrin/discordgo"
"fmt"
"strings"
"fmt"
"strings"
"discord-cfb-bot/internal/clients/cfb" // Adjust this import path as needed
"github.com/bwmarrin/discordgo"
)
var Bot *discordgo.Session
// MessageCreate handles incoming messages and triggers the appropriate response
func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages from the bot itself
if m.Author.ID == s.State.User.ID {
return
}
func Start() error {
var err error
Bot, err = discordgo.New("Bot " + config.BotToken)
// Command for fetching college football scores
if strings.HasPrefix(m.Content, "!cfb") {
// Extract any arguments (e.g., week number) if needed
weekNumber := 10 // Replace this with dynamic parsing if necessary
games, err := cfb.FetchScoreboard(weekNumber)
if err != nil {
return fmt.Errorf("error creating a Discord session: %w", err)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Error fetching games: %s", err))
return
}
fmt.Println("Discord session created successfully")
Bot.AddHandler(commandHandler)
err = Bot.Open()
if err != nil {
return fmt.Errorf("Error opening connection: %w", err)
if len(games) == 0 {
s.ChannelMessageSend(m.ChannelID, "No games found for this week.")
return
}
fmt.Println("Bot is now running.")
return nil
// Send formatted game summaries
for _, game := range games {
gameSummary := cfb.FormatGameOutput(game)
s.ChannelMessageSend(m.ChannelID, gameSummary)
}
}
}
func commandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
}
if strings.HasPrefix(m.Content, "!s ") {
teamName := strings.TrimSpace(strings.TrimPrefix(m.Content, "!s "))
response := clients.GetGameInfo(teamName)
s.ChannelMessageSend(m.ChannelID, response)
}
}