Skip to content

Commit

Permalink
Merge pull request #31 from artificialbutter/staging
Browse files Browse the repository at this point in the history
why did this happen??
  • Loading branch information
artifishvr authored Apr 13, 2024
2 parents 0fe96e3 + 5a57a9d commit 5957c29
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
15 changes: 14 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
FROM node:lts-alpine
FROM node:slim

ENV NODE_ENV=production
# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install google-chrome-stable -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*


RUN npm install -g pnpm

Expand Down
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import fs from 'fs'
import path from 'path';

import { io } from "socket.io-client";
import CharacterAI from 'node_characterai';
import locateChrome from 'locate-chrome';

import * as dotenv from 'dotenv'

dotenv.config()

const channels = process.env.CHANNELIDS.split(",");
Expand All @@ -22,6 +25,10 @@ const client = new Client({
allowedMentions: { parse: [], repliedUser: false }
});

const characterAI = new CharacterAI();
characterAI.requester.puppeteerPath = await new Promise(resolve => locateChrome((arg) => resolve(arg))) || '';
await characterAI.authenticateWithToken(process.env.CHARACTERAI_TOKEN);

// Map to store the last message timestamp per person
const cooldowns = new Map();

Expand Down Expand Up @@ -52,21 +59,19 @@ client.on("messageCreate", async message => {
if (!shouldIReply(message)) return;

try {
if (backendsocket.disconnected) return message.reply(`🔕 Backend socket is not connected. This shouldn't happen! Yell at arti.`);
if (backendsocket.disconnected && message.attachments.size > 0) message.channel.send(`🔕 Backend socket is not connected. Image recognition is disabled.`);
const chat = await characterAI.createOrContinueChat(process.env.CHARACTER_ID);
message.channel.sendTyping();

// Conversation reset
if (message.content.startsWith("%reset")) {
backendsocket.emit("newchat", null);
chat.saveAndStartNewChat()
message.reply(`♻️ Conversation history reset.`);
return;
}

// Update the last message timestamp for the person
cooldowns.set(message.author.id, Date.now());

let imageDetails = '';
if (message.attachments.size > 0) {
if (message.attachments.size > 0 && !backendsocket.disconnected) {
let promises = [];

for (const attachment of message.attachments.values()) {
Expand Down Expand Up @@ -99,23 +104,23 @@ client.on("messageCreate", async message => {
} else {
formattedUserMessage = `${message.author.displayName}: ${message.content}\n${imageDetails}`;
}

message.channel.sendTyping();
let response = await chat.sendAndAwaitResponse(formattedUserMessage, true);

// Handle long responses
if (response.length >= 2000) {
fs.writeFileSync(path.resolve('./how.txt'), response);
if (response.text.length >= 2000) {
fs.writeFileSync(path.resolve('./how.txt'), response.text);
message.reply({ content: "", files: ["./how.txt"], failIfNotExists: false });
return;
}

// Send AI response
message.reply({ content: `${response}`, failIfNotExists: false });
message.reply({ content: `${response.text}`, failIfNotExists: false });
} catch (error) {
console.error(error);
return message.reply(`❌ Error! Yell at arti.`);
}

});

client.login(process.env.DISCORD);
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"dependencies": {
"discord.js": "^14.13.0",
"dotenv": "^16.3.1",
"locate-chrome": "^0.1.1",
"node_characterai": "^1.2.7",
"socket.io-client": "^4.7.2"
},
"devDependencies": {
Expand Down

0 comments on commit 5957c29

Please sign in to comment.