From e144db938b9b750b4799992be337c32184407867 Mon Sep 17 00:00:00 2001 From: En Date: Sat, 18 Jan 2025 14:17:32 +0100 Subject: [PATCH] add index.js --- index.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..21c9748 --- /dev/null +++ b/index.js @@ -0,0 +1,92 @@ +import { finalizeEvent, verifyEvent, getPublicKey, nip19 } from "nostr-tools"; +import { useWebSocketImplementation, Relay } from "nostr-tools/relay"; +import WebSocket from "ws"; +useWebSocketImplementation(WebSocket); + +const TWO_DAYS = 48 * 60 * 60 * 1000; +const RELAY = "TODO"; +const BOT = JSON.stringify({ + name: "BOT", + about: "a little exercise", +}); +let intervalId; + +try { + const args = [...process.argv]; + args.shift(); + args.shift(); + + if (!args.includes("-nsec")) { + console.error("BOT NEEDS NSEC.\n-nsec "); + process.exit(1); + } + + const index = args.findIndex((arg) => arg === "-nsec"); + const nsec = args[index + 1]; + + if (typeof nsec === "undefined" || nsec === "" || !nsec.startsWith("nsec")) { + console.error("BOT NSEC MISSING"); + process.exit(1); + } + + const sk = nip19.decode(nsec).data; + if (!sk) { + console.error("BOT NSEC BAD."); + process.exit(1); + } + + const pk = getPublicKey(sk); + const profileEvent = finalizeEvent( + { + kind: 0, + content: BOT, + created_at: Math.floor(Date.now() / 1000), + tags: [], + }, + sk + ); + const isGood = verifyEvent(profileEvent); + if (isGood) { + (async () => { + const relay = await Relay.connect(RELAY); + await relay.publish(profileEvent); + console.log("IT IS ALIVE!!"); + relay.close(); + })(); + } + + const run = () => { + const today = new Date(); + const dayOfWeek = today.getUTCDay(); + const message = `${dayOfWeek % 6 === 0 ? "gfy" : "GM"} fiatjaf`; + + const event = finalizeEvent( + { + kind: 1, + content: message, + created_at: Math.floor(Date.now() / 1000), + tags: [], + }, + sk + ); + + const isGood = verifyEvent(event); + if (isGood) { + (async () => { + console.log("BOT IS CONNECTING.."); + const relay = await Relay.connect(RELAY); + console.log("BOT CONNECTED"); + await relay.publish(event); + console.log(`[${today.toISOString()}] ${message}`, event); + relay.close(); + })(); + } + }; + + console.log("BOT IS BOTTING as", nip19.npubEncode(pk)); + intervalId = setInterval(run, TWO_DAYS); +} catch (error) { + console.error(`[${new Date().toISOString()}]`, "RIP BOT", error); + clearInterval(intervalId); + process.exit(1); +}