Files
self-bot/Bot/bot.go
T

75 lines
1.8 KiB
Go
Raw Normal View History

2025-03-23 13:38:56 -04:00
package bot
import (
"fmt"
"log"
"os"
"os/signal"
commands "ion606_bot/Bot/Commands"
"github.com/bwmarrin/discordgo"
)
var BotToken string
// commandHandlers maps command names to their interaction handler functions.
var commandHandlers = map[string]func(*discordgo.Session, *discordgo.InteractionCreate){
2025-03-24 16:34:59 -04:00
"meow": commands.HandleMeow,
"purr": commands.HandlePurr,
"boop": commands.HandleBoop,
"hug": commands.HandleHug,
"cuddle": commands.HandleCuddle,
"snuggle": commands.HandleSnuggle,
"catfact": commands.HandleCatfact,
"animalgif": commands.HandleAnimalGif,
"action": commands.HandleAction,
2025-03-23 13:38:56 -04:00
}
// Run starts the Discord bot session and listens for both message and slash command events.
func Run() {
// create a session using the provided bot token.
discord, err := discordgo.New("Bot " + BotToken)
if err != nil {
log.Fatal("Error creating Discord session: ", err)
}
// add event handlers for messages and interactions.
discord.AddHandler(newMessage)
discord.AddHandler(handleInteractionCreate)
err = discord.Open()
if err != nil {
log.Fatal("Error opening Discord session: ", err)
}
RegisterCommands(discord, "")
fmt.Println("Bot running....")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
err = discord.Close()
if err != nil {
log.Println("Error closing Discord session: ", err)
}
}
func newMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// prevent the bot from responding to its own messages.
if m.Author.ID == s.State.User.ID {
return
}
2025-03-24 16:34:59 -04:00
2025-03-23 13:38:56 -04:00
}
func handleInteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
name := i.ApplicationCommandData().Name
if handler, found := commandHandlers[name]; found {
handler(s, i)
} else {
log.Printf("No handler found for command: %v", name)
}
}