77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package bot
|
|
|
|
import (
|
|
"discord-scores-bot/config"
|
|
cfbClient "discord-scores-bot/internal/clients/cfb"
|
|
cbbClient "discord-scores-bot/internal/clients/cbb"
|
|
"github.com/bwmarrin/discordgo"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
var Bot *discordgo.Session
|
|
|
|
func Start() error {
|
|
var err error
|
|
Bot, err = discordgo.New("Bot " + config.BotToken)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating a Discord session: %w", err)
|
|
}
|
|
|
|
fmt.Println("Discord session created successfully")
|
|
|
|
Bot.AddHandler(commandHandler)
|
|
err = Bot.Open()
|
|
if err != nil {
|
|
return fmt.Errorf("error opening connection: %w", err)
|
|
}
|
|
fmt.Println("Bot is now running.")
|
|
return nil
|
|
}
|
|
|
|
func commandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
if m.Author.Bot {
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(m.Content, "!cfb ") {
|
|
teamName := strings.TrimSpace(strings.TrimPrefix(m.Content, "!cfb "))
|
|
response := cfbClient.GetGameInfo(teamName)
|
|
s.ChannelMessageSend(m.ChannelID, response)
|
|
}
|
|
|
|
if strings.HasPrefix(m.Content, "!cbb ") {
|
|
content := strings.TrimSpace(strings.TrimPrefix(m.Content, "!cbb "))
|
|
parts := strings.SplitN(content, " ", 2)
|
|
|
|
teamName := parts[0]
|
|
date := ""
|
|
if len(parts) > 1 {
|
|
if isDateFormat(parts[1]) {
|
|
date = parts[1]
|
|
} else {
|
|
teamName = content // Reset team name to include all parts if no valid date
|
|
}
|
|
}
|
|
|
|
// Check if the team name is a custom name defined in config
|
|
if customTeam, exists := config.CustomTeamNames[strings.ToLower(teamName)]; exists {
|
|
response := cbbClient.GetGameInfo(customTeam, date) // Exact match for custom teams
|
|
s.ChannelMessageSend(m.ChannelID, response)
|
|
} else {
|
|
response := cbbClient.GetGameInfo(teamName, date) // General match for all teams
|
|
s.ChannelMessageSend(m.ChannelID, response)
|
|
}
|
|
}
|
|
}
|
|
|
|
// isDateFormat checks if a string is in the MM/DD format
|
|
func isDateFormat(dateStr string) bool {
|
|
parts := strings.Split(dateStr, "/")
|
|
if len(parts) != 2 {
|
|
return false
|
|
}
|
|
return len(parts[0]) <= 2 && len(parts[1]) <= 2 // Simple check for MM/DD format
|
|
}
|
|
|