commit 273266abb7d206d86ac3e35f6c7df6478f97b707 Author: Zsolt Tasnadi Date: Fri Jan 23 17:54:11 2026 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b03d98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +status-bot +.env \ No newline at end of file diff --git a/crontab b/crontab new file mode 100644 index 0000000..0736c25 --- /dev/null +++ b/crontab @@ -0,0 +1,3 @@ +# crontab +CRON_TZ=Europe/Budapest +0 7 * * 2 curl -X POST http://discord_bot:8080/create-thread diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..bbbb60e --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,24 @@ +version: "3.9" + +services: + bot: + image: golang:1.25.6-alpine + container_name: discord_status_bot + working_dir: /app + volumes: + - .:/app + command: go run main.go + environment: + DISCORD_BOT_TOKEN: ${DISCORD_BOT_TOKEN} + DISCORD_CHANNEL_ID: ${DISCORD_CHANNEL_ID} + ARCHIVE_DURATION: ${ARCHIVE_DURATION} + THREAD_NAME: ${THREAD_NAME} + + cron: + image: alpine + container_name: discord_status_cron + depends_on: + - discord_status_bot + volumes: + - ./crontab:/etc/crontabs/root:ro + command: sh -c "apk add --no-cache curl && crond -f" diff --git a/env-example b/env-example new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/env-example @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0858914 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module status-bot + +go 1.25.6 diff --git a/lib/sender.go b/lib/sender.go new file mode 100644 index 0000000..c80fa04 --- /dev/null +++ b/lib/sender.go @@ -0,0 +1,64 @@ +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 +} diff --git a/lib/server.go b/lib/server.go new file mode 100644 index 0000000..a23b6e5 --- /dev/null +++ b/lib/server.go @@ -0,0 +1,21 @@ +package lib + +import ( + "log" + "net/http" +) + +type StatusBotServer struct { +} + +func (server *StatusBotServer) createThreadHandler(w http.ResponseWriter, r *http.Request) { + bot := &StatusBotSender{} + bot.SendCreateThreadRequest() + w.WriteHeader(http.StatusOK) +} + +func (server *StatusBotServer) Start() { + http.HandleFunc("/create-thread", server.createThreadHandler) + log.Println("Starting server on :8080") + http.ListenAndServe(":8080", nil) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..b400d0e --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +// main.go +package main + +import "status-bot/lib" + +func main() { + server := &lib.StatusBotServer{} + bot := &lib.StatusBotSender{} + bot.SendCreateThreadRequest() + server.Start() +}