This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

Update index.ts

add comments

+20 -10
+20 -10
index.ts
··· 1 1 import { BskyAgent } from "@atproto/api"; 2 - import * as dotenv from "dotenv"; // Added dotenv import 3 - import { CronJob } from "cron"; 4 - import * as process from "process"; 5 - import { generateWolfNoiseString } from "./wolf-noise-generator"; // Import the function 2 + import * as dotenv from "dotenv"; // Import dotenv for loading environment variables 3 + import { CronJob } from "cron"; // Import CronJob for scheduling tasks 4 + import * as process from "process"; // Import process for accessing environment variables 5 + import { generateWolfNoiseString } from "./wolf-noise-generator"; // Import the function for generating wolf noise strings 6 6 7 7 // Load environment variables from the config.env file 8 8 dotenv.config({ path: "./config.env" }); ··· 12 12 service: "https://bsky.social", 13 13 }); 14 14 15 + // Main function for generating and posting wolf noise strings 15 16 async function main() { 16 17 // Check for empty environment variables and abort if needed 17 18 if (!process.env.BLUESKY_USERNAME || !process.env.BLUESKY_PASSWORD) { 18 19 console.error( 19 20 "Missing required environment variables: BLUESKY_USERNAME and BLUESKY_PASSWORD. Aborting script." 20 21 ); 21 - return; // Exit the function if variables are empty 22 + return; 22 23 } 23 24 24 25 try { 26 + // Login to Bluesky 25 27 await agent.login({ 26 28 identifier: process.env.BLUESKY_USERNAME!, 27 29 password: process.env.BLUESKY_PASSWORD!, ··· 33 35 randomNoise = generateWolfNoiseString(); 34 36 } while (randomNoise.trim().length === 0); // Loop until a non-empty string is generated 35 37 38 + // Post the generated string to Bluesky 36 39 if (randomNoise) { 37 40 await agent.post({ 38 41 text: randomNoise.trim(), // Use the generated string (trimmed) 39 42 langs: ["en-US"], 40 43 createdAt: new Date().toISOString(), 41 44 }); 42 - console.log("Just posted:", randomNoise.trim()); 45 + console.log("Just posted:", randomNoise.trim()); // Log the posted string 43 46 } else { 44 47 console.log("Failed to generate a valid wolf noise string after multiple attempts."); 45 48 } ··· 49 52 } 50 53 } 51 54 55 + // Run the main function immediately 52 56 main(); 53 57 54 - // Function to generate a random delay within 1-3 hours (in seconds) 58 + // Function to generate a random delay before the next post 55 59 function getRandomDelay() { 56 60 const minHours = 1; // Minimum hours for delay 57 61 const maxHours = 3; // Maximum hours for delay ··· 68 72 return randomDelay; 69 73 } 70 74 71 - // Run this on a cron job 75 + // Schedule a cron job to run the main function periodically 72 76 const scheduleExpression = "0 * * * *"; // Every hour (used as a base for randomization) 73 77 74 78 const job = new CronJob(scheduleExpression, async () => { 79 + // Calculate a random delay before running the main function 75 80 const delay = getRandomDelay(); 76 - console.log(`Waiting for ${delay / 3600} hours before running main...`); 77 - await new Promise((resolve) => setTimeout(resolve, delay * 1000)); // Wait for the random delay 81 + console.log(`Next post scheduled in approximately ${(delay / 3600).toFixed(2)} hours.`); 82 + 83 + // Wait for the random delay 84 + await new Promise((resolve) => setTimeout(resolve, delay * 1000)); 85 + 86 + // Run the main function 78 87 main(); 79 88 }); 80 89 90 + // Start the cron job 81 91 job.start();