From b098ecca9d2ef1df799ba88f7fd7e9ca56a07d9f Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Sat, 24 Jan 2026 23:30:40 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 ++ Dockerfile | 23 +++++++++++++ cronjob | 2 ++ docker-compose.yml | 10 ++++++ env-example | 5 +++ statusbot.pas | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 cronjob create mode 100644 docker-compose.yml create mode 100644 env-example create mode 100644 statusbot.pas diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbdb475 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +statusbot +statusbot.o diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e1762a4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# Dockerfile - FPC Discord bot SSL támogatással +FROM debian:12-slim + +RUN apt-get update && apt-get install -y \ + fpc fp-units-fcl fp-units-net tzdata curl jq \ + libssl3 libssl-dev \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +COPY statusbot.pas . + +# Fordítás +RUN fpc statusbot.pas + +# Cron telepítése +RUN apt-get update && apt-get install -y cron && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Cron fájl másolása +COPY cronjob /etc/cron.d/discord-cron +RUN chmod 0644 /etc/cron.d/discord-cron && crontab /etc/cron.d/discord-cron + +# Konténer mindig fusson, cron háttérben +CMD ["cron", "-f"] \ No newline at end of file diff --git a/cronjob b/cronjob new file mode 100644 index 0000000..19c2865 --- /dev/null +++ b/cronjob @@ -0,0 +1,2 @@ +# cronjob - minden kedd 9:00 magyar idő szerint +0 9 * * 2 root TZ=Europe/Budapest /app/statusbot diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..31e1ce6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +services: + statusbot: + build: . + container_name: statusbot + environment: + DISCORD_BOT_TOKEN: "${DISCORD_BOT_TOKEN}" + DISCORD_CHANNEL_ID: "${DISCORD_CHANNEL_ID}" + THREAD_NAME: "${THREAD_NAME}" + THREAD_MESSAGE: "${THREAD_MESSAGE}" + ARCHIVE_DURATION: "${ARCHIVE_DURATION}" diff --git a/env-example b/env-example new file mode 100644 index 0000000..c36781e --- /dev/null +++ b/env-example @@ -0,0 +1,5 @@ +DISCORD_BOT_TOKEN=DISCORD_BOT_TOKEN +DISCORD_CHANNEL_ID=DISCORD_CHANNEL_ID +ARCHIVE_DURATION=10080 +THREAD_NAME="Weekly Status" +THREAD_MESSAGE="<@&> Please post your weekly status update here!" \ No newline at end of file diff --git a/statusbot.pas b/statusbot.pas new file mode 100644 index 0000000..43660a9 --- /dev/null +++ b/statusbot.pas @@ -0,0 +1,84 @@ +program DiscordCreateThread; +{$MODE OBJFPC} +{$H+} // Enable AnsiString +uses + SysUtils, Classes, fphttpclient, opensslsockets, DateUtils; + +const + DISCORD_API = 'https://discord.com/api/v10'; + +function GetEnvDef(const Name, Def: string): string; +begin + if GetEnvironmentVariable(Name) = '' then + GetEnvDef := Def + else + GetEnvDef := GetEnvironmentVariable(Name); +end; + +function GetArchiveDuration: Integer; +begin + GetArchiveDuration := StrToIntDef(GetEnvironmentVariable('ARCHIVE_DURATION'), 10080); +end; + +function GetThreadName: string; +var + Base: string; +begin + Base := GetEnvironmentVariable('THREAD_NAME'); + GetThreadName := Base + ' (' + FormatDateTime('yyyy-mm-dd', Now) + ')'; +end; + +function HttpPost(const URL, Body, Token: string): string; +var + Client: TFPHttpClient; + BodyStream, ResponseStream: TStringStream; +begin + Client := TFPHttpClient.Create(nil); + BodyStream := TStringStream.Create(Body); + ResponseStream := TStringStream.Create(''); + try + Client.AddHeader('Authorization', 'Bot ' + Token); + Client.AddHeader('Content-Type', 'application/json'); + Client.RequestBody := BodyStream; + Client.Post(URL, ResponseStream); + HttpPost := ResponseStream.DataString; + finally + ResponseStream.Free; + BodyStream.Free; + Client.Free; + end; +end; + +var + Token, ChannelID, ThreadMessage: string; + ThreadBody, MessageBody, Response: string; + ThreadIDStart, ThreadIDEnd: Integer; + ThreadID: string; + +begin + Token := GetEnvironmentVariable('DISCORD_BOT_TOKEN'); + ChannelID := GetEnvironmentVariable('DISCORD_CHANNEL_ID'); + ThreadMessage := GetEnvironmentVariable('THREAD_MESSAGE'); + + ThreadBody := Format( + '{"name":"%s","auto_archive_duration":%d}', + [StringReplace(GetThreadName,'"','\"',[rfReplaceAll]), GetArchiveDuration] + ); + + Response := HttpPost(DISCORD_API + '/channels/' + ChannelID + '/threads', ThreadBody, Token); + + // egyszerű parsing az id kinyeréséhez + ThreadIDStart := Pos('"id":"', Response); + if ThreadIDStart > 0 then + begin + Inc(ThreadIDStart, 6); + ThreadIDEnd := Pos('"', Response, ThreadIDStart); + ThreadID := Copy(Response, ThreadIDStart, ThreadIDEnd - ThreadIDStart); + + if ThreadID <> '' then + begin + MessageBody := Format('{"content":"%s"}',[StringReplace(ThreadMessage,'"','\"',[rfReplaceAll])]); + HttpPost(DISCORD_API + '/channels/' + ThreadID + '/messages', MessageBody, Token); + end; + end; +end. \ No newline at end of file