This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
76 lines (66 loc) · 2.53 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const venom = require('venom-bot');
const CharacterAI = require('node_characterai');
const characterAIClient = new CharacterAI();
const CHARACTER_ID = 'awbtz.....'; // Replace with your Character ID
const ACCESS_TOKEN = 'cc2cd.....'; // Replace with your Access Token
let chat;
async function initializeCharacterAI() {
console.log('Authenticating Character AI...');
await characterAIClient.authenticateWithToken(ACCESS_TOKEN);
chat = await characterAIClient.createOrContinueChat(CHARACTER_ID);
console.log('Character AI successfully initialized:', chat);
}
async function getCharacterResponse(message) {
console.log('Starting or continuing chat with Character AI...');
let response;
try {
response = await chat.sendAndAwaitResponse(message);
console.log('Character AI response object:', response);
if (Array.isArray(response) && response.length > 0) {
response = response[0]; // Get the first element if the response is an array
}
if (!response || !response.text) {
throw new Error('Character AI did not provide a valid response.');
}
} catch (error) {
console.error('Failed to get response from Character AI:', error);
response = { text: 'Failed to get response from Character AI:' };
}
return response.text;
}
async function start(client) {
console.log('Client successfully created');
await initializeCharacterAI();
client.onMessage(async message => {
try {
if (message.body.startsWith('!')) {
const command = message.body.slice(2);
if (command === 'resetChat') {
await resetChat();
await client.sendText(message.from, 'Chat has been reset.');
} else {
const response = await getCharacterResponse(command);
await client.sendText(message.from, response);
}
}
} catch (error) {
console.error('Failed to send response:', error);
}
});
}
async function resetChat() {
try {
await chat.saveAndStartNewChat();
console.log('Chat successfully reset and new chat started.');
} catch (error) {
console.error('Failed to reset chat:', error);
}
}
venom.create('sessionName', undefined, undefined, {
headless: true,
folderName: 'sessions' // Ensure 'sessions' folder exists in working directory
})
.then(client => start(client))
.catch(error => {
console.error(error);
});