Holy shit this was hard
This commit is contained in:
@ -1,48 +1,76 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"discord-cfb-bot/config"
|
||||
cfbClient "discord-cfb-bot/internal/clients/cfb"
|
||||
cbbClient "discord-cfb-bot/internal/clients/cbb"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"fmt"
|
||||
"strings"
|
||||
"discord-cfb-bot/config"
|
||||
cfbClient "discord-cfb-bot/internal/clients/cfb"
|
||||
cbbClient "discord-cfb-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)
|
||||
}
|
||||
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")
|
||||
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
|
||||
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 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
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, "!cfb ") {
|
||||
teamName := strings.TrimSpace(strings.TrimPrefix(m.Content, "!cfb "))
|
||||
response := cfbClient.GetGameInfo(teamName)
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
// 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)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, "!cbb ") {
|
||||
teamName := strings.TrimSpace(strings.TrimPrefix(m.Content, "!cbb "))
|
||||
response := cbbClient.GetGameInfo(teamName)
|
||||
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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user