···11import { BskyAgent } from "@atproto/api";
22import * as dotenv from "dotenv"; // Import dotenv for loading environment variables
33-import { CronJob } from "cron"; // Import CronJob for scheduling tasks
43import * as process from "process"; // Import process for accessing environment variables
54import { generateWolfNoiseString } from "./wolf-noise-generator";
65···2322 );
2423 return;
2524 }
2525+2626+ console.log("Environment variables loaded successfully.");
26272728 try {
2829 // Login to Bluesky
···4748 });
4849 console.log("Just posted:", randomNoise.trim()); // Log the posted string
4950 } else {
5050- console.log("Failed to generate a valid wolf noise string after multiple attempts.");
5151+ console.log(
5252+ "Failed to generate a valid wolf noise string after multiple attempts."
5353+ );
5154 }
5255 } catch (error) {
5356 console.error("Error during posting:", error);
···7275 return randomDelay;
7376}
74777575-// Schedule a cron job to run the main function periodically
7676-const scheduleExpression = "0 * * * *"; // Every hour (used as a base for randomisation)
7878+// Function to run the main function in a loop with random delays
7979+async function runLoop() {
8080+ while (true) {
8181+ await main();
77827878-const job = new CronJob(scheduleExpression, async () => {
7979- console.log("Cron job triggered.");
8080- // Calculate a random delay before running the main function
8181- const delay = getRandomDelay();
8282- console.log(`Next post scheduled in approximately ${(delay / 3600).toFixed(2)} hours.`);
8383-8484- // Wait for the random delay
8585- await new Promise((resolve) => setTimeout(resolve, delay * 1000));
8686-8787- // Run the main function
8888- await main(); // Ensure to await the main function
8989-});
8383+ // Calculate a random delay before the next iteration
8484+ const delay = getRandomDelay();
8585+ console.log(
8686+ `Next post scheduled in approximately ${(delay / 3600).toFixed(2)} hours.`
8787+ );
90889191-// Start the cron job
9292-console.log("Starting the cron job.");
9393-job.start();8989+ // Wait for the random delay
9090+ await new Promise((resolve) => setTimeout(resolve, delay * 1000));
9191+ }
9292+}
9393+9494+// Start the loop
9595+runLoop().catch((error) => console.error("Error in run loop:", error));