realcoloride / node_characterai

Unofficial Character AI wrapper for node.
https://beta.character.ai/
346 stars 72 forks source link

Error: Already authenticated #107

Closed iKat20 closed 6 months ago

iKat20 commented 1 year ago

I've been trying to use the package on a project that I have, however, I still get the "Already authenticated" error. Here is my code snippet.

const CharacterAI = require("node_characterai")
const characterAI = new CharacterAI()
const pogger = require("pogger")
const config = require("../../config.json")
module.exports = async (message, client) => {
    await characterAI.authenticateWithToken(process.env.CHARACTERAI_TOKEN)

    if (!message.channel.id === config.characters.jax.channel) return message.reply("You can't do this in this channel")
    if (!characterAI.isAuthenticated()) pogger.error("Can't process your request. Unauthenticated.")
    const chat = await characterAI.createOrContinueChat(config.characters.jax.id) 
    const res = await chat.sendAndAwaitResponse(message.content, true)
    message.reply(res.text)

  };
realcoloride commented 1 year ago

"Already authenticated" happens when you call authenticateWithToken() twice without using unauthenticate(). Make sure you are calling authenticateWithToken() once.

Note: if you are executing this function everytime you receive a message (from what I seem to understand), then you are authenticating and creating/continuing chats everytime which is unefficient but also wrong to use. Make sure to use an initialize part to setup cai properly before using it

iKat20 commented 1 year ago

So, if I understand properly, I can initiate it in my index.js once, and that is it?

realcoloride commented 1 year ago

Should be. I don't have your full code but yes. Initialize your client and chat once (or only when needed) and use chat.sendAndAwaitResponse anytime to send a message to your ai.

You need to think of it as if you were on the website.

Parking-Master commented 1 year ago

You are exporting the module as one function, to send a message. Instead of sending the message first, it authenticates first, multiple times when using the function. Move that authenticateWithToken function outside of your module.exports scripts so it only authenticates once.

iKat20 commented 1 year ago

Update :: I did put my Token Authentication in my index file, but, somehow, it doesnt authenticate anymore. Could it be my token who's expired?

Also, tried to export functions in a ./utils/CharacterFunc.js since, if I init a new CharacterAI() I don't find a way to use it through my whole command/event handler

realcoloride commented 10 months ago

Hello, do you still have issues?

iKat20 commented 10 months ago

Affirm! Tried to export it, but didn't figured out yet

Exported it as functions, init in "index", but stays as Already Authenticated/Unauthenticated

realcoloride commented 10 months ago

Could you send your code again please?

iKat20 commented 10 months ago

Deleted it and started from scratch, didn't went back on it yet

realcoloride commented 10 months ago

Ok, make sure you dont authenticate more than once. Happy coding!