realcoloride/node_characterai

AI does not respond to the message

RegisPlayerr opened this issue · 3 comments

I'm trying to use .authenticateWithToken() to create a bot on Discord that responds to the person when a message is sent. But the AI ​​simply doesn't respond to the message, in the console the last thing sent is "[node_characterai] Puppeteer - Done with setup", after that it shows no error and no response

When I use authenticateAsGuest() it works normally without any errors

Below is my bot code:

const CharacterAI = require("node_characterai");
const characterAI = new CharacterAI();
let isAuthenticated = false;
const { Client, IntentsBitField } = require('discord.js');
require('dotenv').config();

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMembers,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.GuildPresences,
    IntentsBitField.Flags.MessageContent,
  ]
})

client.on('ready', () => {
  console.log('bot ready');
})

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  
  if (!isAuthenticated) {
    await characterAI.authenticateWithToken(process.env.CAITOKEN);
    isAuthenticated = true;
  }
  const chat = await characterAI.createOrContinueChat(process.env.CID);

  const response = await chat.sendAndAwaitResponse(message.content, true);

  message.reply(response.text);
})

client.login(process.env.TOKEN);

You are doing the mistake of constantly recreating or continuing a chat every time a message is received. also, I am pretty sure that message.reply is asynchronous, meaning you have to use the await keyword in order to use it.

I made some changes to the code, and now a different error occurs. Sometimes the same error of not responding happens, or sometimes it responds, but when something else is sent after the first message, the error TypeError: Cannot read properties of undefined (reading 'sendAndAwaitResponse') occurs. Below is my modified code:

const CharacterAI = require("node_characterai");
const characterAI = new CharacterAI();
let isAuthenticated = false;
const { Client, IntentsBitField } = require('discord.js');
require('dotenv').config();

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMembers,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.GuildPresences,
    IntentsBitField.Flags.MessageContent,
  ]
})

client.on('ready', () => {
  console.log('bot ready');
})

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  try {
    if (!isAuthenticated) {
      await characterAI.authenticateWithToken(process.env.CAITOKEN);
      var chat = await characterAI.createOrContinueChat(process.env.CID);
      isAuthenticated = true;
    }
    
    const response = await chat.sendAndAwaitResponse(message.content, true);

    await message.reply(response.text);
  } catch (error) {
    console.log(error);
  }
})

client.login(process.env.TOKEN);

Error:

TypeError: Cannot read properties of undefined (reading 'sendAndAwaitResponse')
    at Client.<anonymous> (C:\Users\PC\Documents\Projetos\MarcaoCAI2\index.js:30:33)
    at Client.emit (node:events:514:28)
    at MessageCreateAction.handle (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14)
    at module.exports [as MESSAGE_CREATE] (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
    at WebSocketManager.<anonymous> (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
dex.js:282:31)
    at WebSocketShard.<anonymous> (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\@discordjs\ws\dist\index.js:1173:51)
    at WebSocketShard.emit (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
    at WebSocketShard.onMessage (C:\Users\PC\Documents\Projetos\MarcaoCAI2\node_modules\@discordjs\ws\dist\index.js:988:14) 

You define the chat object before initializing it.

if (!isAuthenticated) {
      await characterAI.authenticateWithToken(process.env.CAITOKEN);
      var chat = await characterAI.createOrContinueChat(process.env.CID);
      isAuthenticated = true;
    }
    // `chat` is not available in this context because it has not been defined elsewhere than above (in the if (!isAuthenticated) condition)
    const response = await chat.sendAndAwaitResponse(message.content, true);

You need to store your chat object somewhere else. Please note that you need to re-use the chat object in order to continue chatting with your character.