program DiscordCreateThread; uses SysUtils, fphttpclient, fpjson, jsonparser, DateUtils; const DISCORD_API = 'https://discord.com/api/v10'; function GetEnvDef(const Name, Def: string): string; begin if GetEnvironmentVariable(Name) = '' then Result := Def else Result := GetEnvironmentVariable(Name); end; function GetArchiveDuration: Integer; begin Result := StrToIntDef(GetEnvironmentVariable('ARCHIVE_DURATION'), 10080); end; function GetThreadName: string; var Base: string; begin Base := GetEnvironmentVariable('THREAD_NAME'); Result := Base + ' (' + FormatDateTime('yyyy-mm-dd', Now) + ')'; end; function HttpPost(const URL, Body, Token: string): string; var Client: TFPHttpClient; Resp: TStringStream; begin Client := TFPHttpClient.Create(nil); Resp := TStringStream.Create(''); try Client.AddHeader('Authorization', 'Bot ' + Token); Client.AddHeader('Content-Type', 'application/json'); Client.RequestBody := TStringStream.Create(Body); Client.Post(URL, Resp); Result := Resp.DataString; finally Client.Free; Resp.Free; end; end; var Token, ChannelID, ThreadMessage: string; ThreadBody, MessageBody, Response: string; JSON, MsgJSON: TJSONObject; Parser: TJSONData; ThreadID: string; begin Token := GetEnvironmentVariable('DISCORD_BOT_TOKEN'); ChannelID := GetEnvironmentVariable('DISCORD_CHANNEL_ID'); ThreadMessage := GetEnvironmentVariable('THREAD_MESSAGE'); JSON := TJSONObject.Create; JSON.Add('name', GetThreadName); JSON.Add('auto_archive_duration', GetArchiveDuration); ThreadBody := JSON.AsJSON; JSON.Free; Response := HttpPost( DISCORD_API + '/channels/' + ChannelID + '/threads', ThreadBody, Token ); Parser := GetJSON(Response); ThreadID := Parser.FindPath('id').AsString; Parser.Free; if ThreadID <> '' then begin MsgJSON := TJSONObject.Create; MsgJSON.Add('content', ThreadMessage); MessageBody := MsgJSON.AsJSON; MsgJSON.Free; HttpPost( DISCORD_API + '/channels/' + ThreadID + '/messages', MessageBody, Token ); end; end.