package main import ( "himbot/command" "himbot/lib" "log" "os" "os/signal" "syscall" "time" "github.com/bwmarrin/discordgo" "github.com/joho/godotenv" ) var ( commands = []*discordgo.ApplicationCommand{ { Name: "ping", Description: "ping pong!", }, { Name: "hs", Description: "This command was your nickname in highschool!", Options: []*discordgo.ApplicationCommandOption{ { Type: discordgo.ApplicationCommandOptionString, Name: "nickname", Description: "Your nickname in highschool.", Required: true, }, }, }, { Name: "markov", Description: "Why did the Markov chain break up with its partner? Because it couldn't handle the past!", Options: []*discordgo.ApplicationCommandOption{ { Type: discordgo.ApplicationCommandOptionInteger, Name: "messages", Description: "Number of messages to use (default: 100, max: 1000)", Required: false, }, }, }, { Name: "himbucks", Description: "Check your himbucks balance", }, { Name: "himboard", Description: "View the himbucks leaderboard", }, { Name: "sendbucks", Description: "Send himbucks to another user", Options: []*discordgo.ApplicationCommandOption{ { Type: discordgo.ApplicationCommandOptionUser, Name: "user", Description: "The user to send himbucks to", Required: true, }, { Type: discordgo.ApplicationCommandOptionInteger, Name: "amount", Description: "Amount of himbucks to send", Required: true, MinValue: &[]float64{1}[0], }, }, }, } commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ "ping": lib.HandleCommand("ping", 5*time.Second, command.PingCommand), "hs": lib.HandleCommand("hs", 10*time.Second, command.HsCommand), "markov": lib.HandleCommand("markov", 30*time.Second, command.MarkovCommand), "himbucks": lib.HandleCommand("himbucks", 5*time.Second, command.BalanceGetCommand), "himboard": lib.HandleCommand("himboard", 5*time.Second, command.LeaderboardCommand), "sendbucks": lib.HandleCommand("sendbucks", 1800*time.Second, command.BalanceSendCommand), } ) func main() { godotenv.Load(".env") err := lib.InitDB() if err != nil { log.Fatalf("Failed to initialize database: %v", err) } token := os.Getenv("DISCORD_TOKEN") if token == "" { log.Fatalln("No $DISCORD_TOKEN given.") } dg, err := discordgo.New("Bot " + token) if err != nil { log.Fatalf("Error creating Discord session: %v", err) } dg.AddHandler(ready) dg.AddHandler(interactionCreate) processorManager := lib.NewMessageProcessorManager() // Register processors processorManager.RegisterProcessor(lib.ProcessHimbucks) dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { processorManager.ProcessMessage(s, m) }) dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages err = dg.Open() if err != nil { log.Fatalf("Error opening connection: %v", err) } log.Println("Bot is now running. Press CTRL-C to exit.") registerCommands(dg) sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc if lib.DBClient != nil { lib.DBClient.Close() } dg.Close() } func ready(s *discordgo.Session, event *discordgo.Ready) { log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator) } func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { h(s, i) } } func registerCommands(s *discordgo.Session) { // First, delete all existing commands log.Println("Deleting existing commands...") existingCommands, err := s.ApplicationCommands(s.State.User.ID, "") if err != nil { log.Printf("Error fetching existing commands: %v", err) } for _, cmd := range existingCommands { err := s.ApplicationCommandDelete(s.State.User.ID, "", cmd.ID) if err != nil { log.Printf("Error deleting command %s: %v", cmd.Name, err) } } // Then register the new commands log.Println("Registering new commands...") registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) for i, v := range commands { cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v) if err != nil { log.Panicf("Cannot create '%v' command: %v", v.Name, err) } registeredCommands[i] = cmd } }