package lib import ( "bytes" "encoding/json" "log" "net/http" "os" "strconv" "time" ) const DISCORD_API = "https://discord.com/api/v10" type ThreadRequest struct { Name string `json:"name"` Auto_archive_duration int `json:"auto_archive_duration"` } type StatusBotSender struct { } func (bot *StatusBotSender) GetArchiveDuration() int { v, err := strconv.Atoi(os.Getenv("ARCHIVE_DURATION")) if err != nil { return 10080 } return v } func (bot *StatusBotSender) GetThreadName() string { loc, _ := time.LoadLocation("Europe/Budapest") base := os.Getenv("THREAD_NAME") date := time.Now().In(loc).Format("2006-01-02") return base + " (" + date + ")" } func (bot *StatusBotSender) BuildBody() ([]byte, error) { return json.Marshal(ThreadRequest{ Name: bot.GetThreadName(), Auto_archive_duration: bot.GetArchiveDuration(), }) } func (bot *StatusBotSender) BuildRequest(body []byte) (*http.Request, error) { req, err := http.NewRequest( "POST", DISCORD_API+"/channels/"+os.Getenv("DISCORD_CHANNEL_ID")+"/threads", bytes.NewBuffer(body), ) req.Header.Set("Authorization", "Bot "+os.Getenv("DISCORD_BOT_TOKEN")) req.Header.Set("Content-Type", "application/json") return req, err } func (bot *StatusBotSender) SendCreateThreadRequest() error { body, _ := bot.BuildBody() req, _ := bot.BuildRequest(body) log.Println(req.Method, req.URL.String(), req.Header.Values("Authorization")) log.Println("Sending request to create thread:", string(body)) res, _ := http.DefaultClient.Do(req) log.Println("Response status:", res.Status) return nil }