NukaNewsBot/commands/com_handler.go

32 lines
814 B
Go
Raw Normal View History

2024-04-28 02:30:53 +00:00
package commands
import (
"strings"
"github.com/bwmarrin/discordgo"
)
// This function will be called whenever a new message is created
func CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// Check if the message starts with a command prefix
if strings.HasPrefix(m.Content, "!") {
// Extract the command and arguments
parts := strings.Fields(m.Content)
command := parts[0]
// Find the corresponding handler function in the map
if handlerFunc, ok := commandMap[command]; ok {
// Call the handler function
handlerFunc(s, m)
} else {
// Respond to unknown commands
_, _ = s.ChannelMessageSend(m.ChannelID, "Unknown command. Type !help for a list of commands.")
}
}
}