This repository has no description
1import { BskyAgent } from "@atproto/api";
2import * as dotenv from "dotenv"; // Import dotenv for loading environment variables
3import * as process from "process"; // Import process for accessing environment variables
4import { generateWolfNoiseString } from "./wolf-noise-generator";
5
6// Load environment variables from the config.env file
7dotenv.config({ path: "./src/config.env" });
8
9// Create a Bluesky Agent
10const agent = new BskyAgent({
11 service: "https://bsky.social",
12});
13
14// Function to get the maximum delay hours from environment
15function getMaxDelayHours() {
16 return parseInt(process.env.MAX_DELAY_HOURS) || 6; // Default to 6 if not set or invalid
17}
18
19// Function to get the minimum delay hours from environment
20function getMinDelayHours() {
21 return parseInt(process.env.MIN_DELAY_HOURS) || 1; // Default to 1 if not set or invalid
22}
23
24// Function to generate a random delay before the next post
25function getRandomDelay() {
26 const minHours = getMinDelayHours(); // Get minimum delay hours
27 const maxHours = getMaxDelayHours(); // Get maximum delay hours
28
29 // Convert hours to seconds
30 const minDelaySeconds = minHours * 60 * 60;
31 const maxDelaySeconds = maxHours * 60 * 60;
32
33 let randomDelay;
34 do {
35 // Generate a random number within the desired range (inclusive)
36 randomDelay =
37 Math.floor(Math.random() * (maxDelaySeconds - minDelaySeconds + 1)) +
38 minDelaySeconds;
39 } while (randomDelay < minDelaySeconds || randomDelay > maxDelaySeconds);
40
41 return randomDelay;
42}
43
44// Main function for generating and posting wolf noise strings
45async function main() {
46 console.log("Main function called.");
47
48 // Check for empty environment variables and abort if needed
49 if (!process.env.BLUESKY_USERNAME || !process.env.BLUESKY_PASSWORD) {
50 console.error(
51 "Missing required environment variables: BLUESKY_USERNAME and BLUESKY_PASSWORD.\nAborting script."
52 );
53 process.exit(1);
54 }
55
56 console.log("Environment variables loaded successfully.");
57
58 try {
59 // Login to Bluesky
60 await agent.login({
61 identifier: process.env.BLUESKY_USERNAME!,
62 password: process.env.BLUESKY_PASSWORD!,
63 });
64 console.log("Logged in to Bluesky.");
65
66 // Generate a random wolf noise string
67 let randomNoise;
68 do {
69 randomNoise = generateWolfNoiseString();
70 } while (randomNoise.trim().length === 0); // Loop until a non-empty string is generated
71
72 // Post the generated string to Bluesky
73 if (randomNoise) {
74 await agent.post({
75 text: randomNoise.trim(), // Use the generated string (trimmed)
76 langs: ["en-US"],
77 createdAt: new Date().toISOString(),
78 });
79 console.log("Just posted:", randomNoise.trim()); // Log the posted string
80 } else {
81 console.log(
82 "Failed to generate a valid wolf noise string after multiple attempts."
83 );
84 }
85 } catch (error) {
86 console.error("Error during posting:", error);
87 // You can optionally implement retry logic or notify someone here
88 }
89}
90
91// Function to run the main function in a loop with random delays
92async function runLoop() {
93 while (true) {
94 await main();
95
96 // Calculate a random delay before the next iteration
97 const delay = getRandomDelay();
98
99 // Calculate delay in whole hours and minutes
100 const hours = Math.floor(delay / 3600); // Use 3600 for hours
101 const minutes = Math.floor((delay % 3600) / 60); // Calculate remaining minutes
102
103 // Format the delay string with hours and minutes (no decimals)
104 const formattedDelay = `${
105 hours > 0 ? hours + " hour" + (hours > 1 ? "s" : "") : ""
106 }${hours > 0 && minutes > 0 ? " " : ""}${
107 minutes > 0 ? minutes + " minute" + (minutes > 1 ? "s" : "") : ""
108 }`;
109
110 console.log(`Next post scheduled in approximately ${formattedDelay}.`);
111
112 // Wait for the random delay
113 await new Promise((resolve) => setTimeout(resolve, delay * 1000));
114 }
115}
116
117// Start the loop
118runLoop().catch((error) => console.error("Error in run loop:", error));