import "dotenv/config"; import { AtpAgent, AtUri } from "@atproto/api"; import { OAuthClient } from "@atproto/oauth-client"; import { Response } from "@atproto/api/dist/client/types/com/atproto/server/createSession"; const BSKY_HANDLE = process.env.BSKY_HANDLE ?? (() => { throw new Error("Missing BSKY_HANDLE in environment variable"); })(); const BSKY_API_KEY = process.env.BSKY_API_KEY ?? (() => { throw new Error("Missing BSKY_API_KEY in environment variable"); })(); async function Authenticate (): Promise { const agent = new AtpAgent({ service: "https://bsky.social" }); await agent.login({ identifier: BSKY_HANDLE, password: BSKY_API_KEY }); return agent; } async function ResolveHandleToDid(agent: AtpAgent, handle: string): Promise { const response = await agent.resolveHandle({ handle }); if (!response || typeof response !== "object" || !response.data || !response.data.did) { throw new Error(`Failed to resolve handle to DID: Unexpected response structure for handle ${handle}`); } const userDid = response.data.did; console.log(`Resolved handle ${handle} to DID: ${userDid}`); return userDid; } async function BlockUser(agent: AtpAgent, userDid: string) { const { uri } = await agent.app.bsky.graph.block.create( { repo: agent.session?.did }, { subject: userDid, createdAt: new Date().toISOString(), } ); console.log(`Blocked user with DID: ${userDid}`); } async function GetBlockRecordKey(agent: AtpAgent, userDid: string): Promise { const blocks = await agent.app.bsky.graph.block.list({ repo: agent.session!.did, }); const blockRecord = blocks.records.find(record => record.value.subject === userDid); if (blockRecord) { return blockRecord.uri; } else { console.log(`No block record found for user: ${userDid}`); return null; } } async function UnblockUser(agent: AtpAgent, userDid: string) { const blockUri = await GetBlockRecordKey(agent, userDid); if (!blockUri) return; // Get the record key by parsing the URI const { rkey } = new AtUri(blockUri); // Delete the block record await agent.app.bsky.graph.block.delete({ repo: agent.session?.did, rkey, }); } async function Main () { const agent = await Authenticate(); const userHandle = "tundrashark.com"; const userDid = await ResolveHandleToDid(agent, userHandle); await BlockUser(agent, userDid); // await UnblockUser(agent, userDid); } Main();