128 lines
3.2 KiB
Go
128 lines
3.2 KiB
Go
package bot
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"time"
|
|
|
|
commands "ion606_bot/Bot/Commands"
|
|
helpers "ion606_bot/Bot/Helpers"
|
|
|
|
"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){
|
|
"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,
|
|
"suggest": commands.HandleSuggest,
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
_, err = helpers.InitSuggestionDB()
|
|
if err != nil {
|
|
log.Printf("Failed to initialize suggestion DB: %v", err)
|
|
}
|
|
|
|
go func() {
|
|
ticker := time.NewTicker(5 * time.Minute) // adjust interval as needed
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
// For example, sweep entries older than 5 minutes:
|
|
if err := helpers.SweepExpiredSubmissions(5 * time.Minute); err != nil {
|
|
log.Printf("Error sweeping expired submissions: %v", err)
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
RegisterCommands(discord, "")
|
|
|
|
log.Println("Bot running....")
|
|
|
|
// Wait for interrupt signal.
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt)
|
|
<-c
|
|
|
|
err = helpers.CloseDB()
|
|
if err != nil {
|
|
log.Println("Error closing suggestion DB:", err)
|
|
}
|
|
|
|
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) {
|
|
// Check for modal submissions first.
|
|
if i.Type == discordgo.InteractionModalSubmit {
|
|
if strings.HasPrefix(i.ModalSubmitData().CustomID, "reactionrole_modal:") {
|
|
commands.HandleReactionRoleModalSubmit(s, i)
|
|
return
|
|
}
|
|
|
|
if i.ModalSubmitData().CustomID == "suggestion_modal" {
|
|
commands.HandleSuggestModal(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)
|
|
}
|
|
}
|
|
}
|