···11import { BskyAgent } from "@atproto/api";
22-import * as dotenv from "dotenv"; // Added dotenv import
33-import { CronJob } from "cron";
44-import * as process from "process";
55-import { generateWolfNoiseString } from "./wolf-noise-generator"; // Import the function
22+import * as dotenv from "dotenv"; // Import dotenv for loading environment variables
33+import { CronJob } from "cron"; // Import CronJob for scheduling tasks
44+import * as process from "process"; // Import process for accessing environment variables
55+import { generateWolfNoiseString } from "./wolf-noise-generator"; // Import the function for generating wolf noise strings
6677// Load environment variables from the config.env file
88dotenv.config({ path: "./config.env" });
···1212 service: "https://bsky.social",
1313});
14141515+// Main function for generating and posting wolf noise strings
1516async function main() {
1617 // Check for empty environment variables and abort if needed
1718 if (!process.env.BLUESKY_USERNAME || !process.env.BLUESKY_PASSWORD) {
1819 console.error(
1920 "Missing required environment variables: BLUESKY_USERNAME and BLUESKY_PASSWORD. Aborting script."
2021 );
2121- return; // Exit the function if variables are empty
2222+ return;
2223 }
23242425 try {
2626+ // Login to Bluesky
2527 await agent.login({
2628 identifier: process.env.BLUESKY_USERNAME!,
2729 password: process.env.BLUESKY_PASSWORD!,
···3335 randomNoise = generateWolfNoiseString();
3436 } while (randomNoise.trim().length === 0); // Loop until a non-empty string is generated
35373838+ // Post the generated string to Bluesky
3639 if (randomNoise) {
3740 await agent.post({
3841 text: randomNoise.trim(), // Use the generated string (trimmed)
3942 langs: ["en-US"],
4043 createdAt: new Date().toISOString(),
4144 });
4242- console.log("Just posted:", randomNoise.trim());
4545+ console.log("Just posted:", randomNoise.trim()); // Log the posted string
4346 } else {
4447 console.log("Failed to generate a valid wolf noise string after multiple attempts.");
4548 }
···4952 }
5053}
51545555+// Run the main function immediately
5256main();
53575454-// Function to generate a random delay within 1-3 hours (in seconds)
5858+// Function to generate a random delay before the next post
5559function getRandomDelay() {
5660 const minHours = 1; // Minimum hours for delay
5761 const maxHours = 3; // Maximum hours for delay
···6872 return randomDelay;
6973}
70747171-// Run this on a cron job
7575+// Schedule a cron job to run the main function periodically
7276const scheduleExpression = "0 * * * *"; // Every hour (used as a base for randomization)
73777478const job = new CronJob(scheduleExpression, async () => {
7979+ // Calculate a random delay before running the main function
7580 const delay = getRandomDelay();
7676- console.log(`Waiting for ${delay / 3600} hours before running main...`);
7777- await new Promise((resolve) => setTimeout(resolve, delay * 1000)); // Wait for the random delay
8181+ console.log(`Next post scheduled in approximately ${(delay / 3600).toFixed(2)} hours.`);
8282+8383+ // Wait for the random delay
8484+ await new Promise((resolve) => setTimeout(resolve, delay * 1000));
8585+8686+ // Run the main function
7887 main();
7988});
80899090+// Start the cron job
8191job.start();