skyware-js / bot

A framework for building bots on Bluesky.
Mozilla Public License 2.0
20 stars 2 forks source link

Problem using typescript #6

Closed Cristuker closed 1 week ago

Cristuker commented 1 week ago

I have this source code

import { AtpAgent, AtpSessionData } from "@atproto/api";
import { getSession, saveSession } from "../redis";
import { Bot } from "@skyware/bot";
import "dotenv/config";

export const generateAgentAndBot = async (): Promise<[AtpAgent,Bot]> => {
  const bot = new Bot();
  const agent = new AtpAgent({
    service: "https://bsky.social",
    persistSession: async (evt: string, session: AtpSessionData | undefined) => {
      await saveSession(JSON.stringify(session));
      console.log('Persist session');
    }
  });

  const session = await getSession();

  if (session) {
    console.log("Resume session");
    await agent.resumeSession(JSON.parse(session));
    await bot.resumeSession(JSON.parse(session))
    return [agent, bot];
  }

  const  { data } = await agent.login({
    identifier: String(process.env.IDENTIFIER),
    password: String(process.env.PASSWORD),
  });
  await bot.resumeSession({
    refreshJwt: data.refreshJwt,
    accessJwt: data.accessJwt,
    handle: data.handle,
    did: data.did,
    email: data.email,
    emailConfirmed: data.emailConfirmed,
    emailAuthFactor: data.emailAuthFactor,
    active: !!data.active,
    status: data.status
  })

  console.log("Create a new Session");

  return [agent, bot];
};

and when I run I receive this error on terminal


/home/cristian/github/save-to-later/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/cristian/github/save-to-later/node_modules/@skyware/bot/dist/index.js from /home/cristian/github/save-to-later/src/config/agentProxy.ts not supported.
Instead change the require of index.js in /home/cristian/github/save-to-later/src/config/agentProxy.ts to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/home/cristian/github/save-to-later/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/home/cristian/github/save-to-later/src/config/agentProxy.ts:15:15)
    at m._compile (/home/cristian/github/save-to-later/node_modules/ts-node/dist/index.js:857:29) {
  code: 'ERR_REQUIRE_ESM'
}
[nodemon] app crashed - waiting for file changes before starting...
futurGH commented 1 week ago

As the error says, the library only supports ESM. You'll need to set up your tsconfig accordingly.

You can also access bot.agent instead of initializing your own.

Cristuker commented 1 week ago

Well I do some research and I have same error yet. SUggestions?

{
  "compilerOptions": {
    "target": "ESNext",    
    "lib": ["es6"],                                      
    "module": "commonjs",                                
    "rootDir": "src",                                    
    "resolveJsonModule": true,                          
    "allowJs": false,                                  
    "outDir": "build",                                  
    "esModuleInterop": true,                            
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "noImplicitAny": false,                               
    "skipLibCheck": true,  
    "isolatedModules": true,
    "moduleResolution": "Node",

  }
}
Cristuker commented 1 week ago

As the error says, the library only supports ESM. You'll need to set up your tsconfig accordingly.

You can also access bot.agent instead of initializing your own.

I will do this

futurGH commented 1 week ago

Try "module": "esnext". You can look up TypeScript configs for Node with ESM — this isn't really relevant to the library, so I'm going to close this issue.

Cristuker commented 1 week ago

I'm trying to use this with TS and I'm find some problems. So yes it's really relevant. I don't have this kind of problem with others lib

futurGH commented 1 week ago

Like I said, there are plenty of guides available online to help you set up your project for ESM, here's one.

Cristuker commented 1 week ago

instead of initializing your own.

I want but I can't pass

persistSession: async (evt: string, session: AtpSessionData | undefined) => {
      await saveSession(JSON.stringify(session));
      console.log('Persist session');
    }

to bot agent. I need do this to save my session and avoid break rate limit

futurGH commented 1 week ago

You can persist the result of Bot.login() and pass that to resumeSession — will add that as an option though.