package main import ( "bytes" "encoding/json" "fmt" "log" "net/http" "os" "os/signal" "strconv" "strings" "syscall" "time" "thoughtful-dcbob/commands" "github.com/bwmarrin/discordgo" ) func main() { // Create a new Discord session using the provided bot token. token := os.Getenv("DISCORD_BOT_TOKEN") if token == "" { fmt.Println("DISCORD_BOT_TOKEN not set; aborting") return } dg, err := discordgo.New("Bot " + token) if err != nil { fmt.Println("error creating Discord session,", err) return } // Register handlers dg.AddHandler(messageCreate) dg.AddHandler(func(s *discordgo.Session, ic *discordgo.InteractionCreate) { commands.HandleInteraction(s, ic) }) // Just like the ping pong example, we only care about receiving message // events in this example. dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentsMessageContent // Open a websocket connection to Discord and begin listening. err = dg.Open() if err != nil { fmt.Println("error opening connection,", err) return } // After opening, register slash commands. if err := commands.RegisterAll(dg); err != nil { fmt.Println("error registering commands:", err) // continue running; commands may be registered later } // Start Kuma push monitor if configured kumaPushURL := os.Getenv("Kuma-Push-Url") if kumaPushURL != "" { intervalStr := os.Getenv("Kuma-Push-Interval") interval := 60 // default to 60 seconds if intervalStr != "" { if parsed, err := strconv.Atoi(intervalStr); err == nil && parsed > 0 { interval = parsed } } go startKumaPush(kumaPushURL, interval) log.Printf("Kuma push monitor started (URL: %s, Interval: %ds)", kumaPushURL, interval) } // Wait here until CTRL-C or other term signal is received. fmt.Println("Bot is now running. Press CTRL-C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc // Cleanly close down the Discord session. dg.Close() } // startKumaPush periodically sends GET requests to the Kuma push URL func startKumaPush(url string, intervalSeconds int) { ticker := time.NewTicker(time.Duration(intervalSeconds) * time.Second) defer ticker.Stop() for range ticker.C { pushToKuma(url) } } // pushToKuma sends a GET request to the Kuma push URL with retry logic func pushToKuma(url string) { client := &http.Client{Timeout: 10 * time.Second} // First attempt resp, err := client.Get(url) if err != nil { log.Printf("Kuma push failed: %v (retrying in 15s)", err) time.Sleep(15 * time.Second) // Retry attempt resp, err = client.Get(url) if err != nil { log.Printf("Kuma push retry failed: %v", err) return } defer resp.Body.Close() log.Printf("Kuma push retry succeeded: status %d", resp.StatusCode) return } defer resp.Body.Close() log.Printf("Kuma push: status %d", resp.StatusCode) } // This function will be called (due to AddHandler above) every time a new // message is created on any channel that the authenticated bot has access to. // // It is called whenever a message is created but only when it's sent through a // server as we did not request IntentsDirectMessages. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Ignore all messages created by the bot itself // This isn't required in this specific example but it's a good practice. if m.Author.ID == s.State.User.ID { return } // Non-slash command: .thought Title; Description if strings.HasPrefix(m.Content, ".thought") { cfg, ok, err := commands.GetUserConfig(m.Author.ID) if err != nil { s.ChannelMessageSend(m.ChannelID, "Storage error.") return } if !ok { s.ChannelMessageSend(m.ChannelID, "Please run `/setup` first to configure your Thoughtful instance.") return } rest := strings.TrimSpace(m.Content[len(".thought"):]) if rest == "" { s.ChannelMessageSend(m.ChannelID, "Usage: .thought Title; Description") return } parts := strings.SplitN(rest, ";", 2) title := strings.TrimSpace(parts[0]) desc := "" if len(parts) > 1 { desc = strings.TrimSpace(parts[1]) } payload := map[string]string{"title": title, "description": desc} b, _ := json.Marshal(payload) req, _ := http.NewRequest("POST", cfg.InstanceURL+"/api/ideas/create", bytes.NewReader(b)) req.Header.Set("API-Authentication", cfg.APIKey) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { s.ChannelMessageSend(m.ChannelID, "Failed to send to API: "+err.Error()) return } defer resp.Body.Close() if resp.StatusCode == 200 { s.ChannelMessageSend(m.ChannelID, "Thought created successfully!") } else { s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Failed to create thought. Status: %d", resp.StatusCode)) } return } }