Files
self-bot/Bot/bot.go
T

108 lines
2.8 KiB
Go
Raw Normal View History

2025-03-23 13:38:56 -04:00
package bot
import (
"log"
"os"
"os/signal"
2025-04-07 12:27:19 -04:00
"strings"
2025-03-23 13:38:56 -04:00
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-04-07 12:27:19 -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,
"reactionrole": commands.HandleReactionRole,
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)
}
2025-04-07 16:16:27 -04:00
// Load reaction role target map from file.
err = commands.LoadReactionRoleTarget("reactionRoleTarget.json")
if err != nil {
log.Printf("No reaction role target config loaded: %v", err)
}
2025-03-23 13:38:56 -04:00
// 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, "")
2025-04-07 16:16:27 -04:00
log.Println("Bot running....")
// Wait for interrupt signal.
2025-03-23 13:38:56 -04:00
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
2025-04-07 16:16:27 -04:00
// Save reaction role target map to file on shutdown.
err = commands.SaveReactionRoleTarget("reactionRoleTarget.json")
if err != nil {
log.Println("Error saving reaction role target:", err)
}
2025-03-23 13:38:56 -04:00
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
}
}
func handleInteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
2025-04-07 12:27:19 -04:00
// Check for modal submissions first.
if i.Type == discordgo.InteractionModalSubmit {
if strings.HasPrefix(i.ModalSubmitData().CustomID, "reactionrole_modal:") {
commands.HandleReactionRoleModalSubmit(s, i)
return
}
}
// Then check for message components (buttons).
if i.Type == discordgo.InteractionMessageComponent {
if strings.HasPrefix(i.MessageComponentData().CustomID, "rr:") {
commands.HandleReactionRoleButton(s, i)
return
}
}
// Finally, process application commands.
if i.Type == discordgo.InteractionApplicationCommand {
name := i.ApplicationCommandData().Name
if handler, found := commandHandlers[name]; found {
handler(s, i)
} else {
log.Printf("No handler found for command: %v", name)
}
2025-03-23 13:38:56 -04:00
}
}