This repository has no description
1import { BskyAgent } from "@atproto/api";
2import * as dotenv from "dotenv"; // Import dotenv for loading environment variables
3import { CronJob } from "cron"; // Import CronJob for scheduling tasks
4import * as process from "process"; // Import process for accessing environment variables
5import { generateWolfNoiseString } from "./wolf-noise-generator"; // Import the function for generating wolf noise strings
6
7// Load environment variables from the config.env file
8dotenv.config({ path: "./config.env" });
9
10// Create a Bluesky Agent
11const agent = new BskyAgent({
12 service: "https://bsky.social",
13});
14
15// Main function for generating and posting wolf noise strings
16async function main() {
17 // Check for empty environment variables and abort if needed
18 if (!process.env.BLUESKY_USERNAME || !process.env.BLUESKY_PASSWORD) {
19 console.error(
20 "Missing required environment variables: BLUESKY_USERNAME and BLUESKY_PASSWORD. Aborting script."
21 );
22 return;
23 }
24
25 try {
26 // Login to Bluesky
27 await agent.login({
28 identifier: process.env.BLUESKY_USERNAME!,
29 password: process.env.BLUESKY_PASSWORD!,
30 });
31
32 // Generate a random wolf noise string
33 let randomNoise;
34 do {
35 randomNoise = generateWolfNoiseString();
36 } while (randomNoise.trim().length === 0); // Loop until a non-empty string is generated
37
38 // Post the generated string to Bluesky
39 if (randomNoise) {
40 await agent.post({
41 text: randomNoise.trim(), // Use the generated string (trimmed)
42 langs: ["en-US"],
43 createdAt: new Date().toISOString(),
44 });
45 console.log("Just posted:", randomNoise.trim()); // Log the posted string
46 } else {
47 console.log("Failed to generate a valid wolf noise string after multiple attempts.");
48 }
49 } catch (error) {
50 console.error("Error during posting:", error);
51 // You can optionally implement retry logic or notify someone here
52 }
53}
54
55// Run the main function immediately
56main();
57
58// Function to generate a random delay before the next post
59function getRandomDelay() {
60 const minHours = 1; // Minimum hours for delay
61 const maxHours = 3; // Maximum hours for delay
62
63 // Convert hours to seconds
64 const minDelaySeconds = minHours * 60 * 60;
65 const maxDelaySeconds = maxHours * 60 * 60;
66
67 // Generate a random number within the desired range (inclusive)
68 const randomDelay =
69 Math.floor(Math.random() * (maxDelaySeconds - minDelaySeconds + 1)) +
70 minDelaySeconds;
71
72 return randomDelay;
73}
74
75// Schedule a cron job to run the main function periodically
76const scheduleExpression = "0 * * * *"; // Every hour (used as a base for randomization)
77
78const job = new CronJob(scheduleExpression, async () => {
79 // Calculate a random delay before running the main function
80 const delay = getRandomDelay();
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
87 main();
88});
89
90// Start the cron job
91job.start();