Files
drb-server/discordBot/addons/gptInteraction.mjs
Logan Cusano 961a7cc2bd
All checks were successful
release-tag / release-image (push) Successful in 4m19s
DRB Tests / drb_mocha_tests (push) Successful in 1m16s
Implement early AI integration #18
- Added new event to catch messageCreate events
- @ messages to the server will use ChatGPT to respond to the message with an indepth prompt about the server
- Implement module to interact with chatGPT repeatably
- Add linkcop with GPT integration #12
- Added environment variable for inital prompt for GPT integration
2024-06-02 19:16:01 -04:00

53 lines
1.8 KiB
JavaScript

import { DebugBuilder } from "../../modules/debugger.mjs";
const log = new DebugBuilder("server", "discordBot.addons.gptInteraction");
import { gptHandler } from "../modules/gptHandler.mjs";
export const gptInteraction = async (nodeIo, message) => {
let conversation = [];
let prevMessages = await message.channel.messages.fetch({ limit: 10 });
prevMessages.reverse();
prevMessages.forEach((msg) => {
// Check if the message was sent within the last 24 hours
if (new Date().getTime() - msg.createdTimestamp > (24 * 60 * 60 * 1000)) {
return;
}
// Check if it's from a bot other than the server
if (msg.author.bot && msg.author.id !== nodeIo.serverClient.user.id) return;
const username = msg.author.username.replace(/\s+/g, '_').replace(/[^\w\s]/gi, '');
if (msg.author.id === nodeIo.serverClient.user.id) {
conversation.push({
role: 'assistant',
name: msg.author.id,
content: msg.content,
});
return;
}
conversation.push({
role: 'user',
name: msg.author.id,
content: msg.content.replace(`<@${nodeIo.serverClient.user.id}>`, ''),
});
});
const response = await gptHandler(conversation);
if (response) {
const responseMessage = response.choices[0].message.content;
const chunkSize = 2500;
for (let i = 0; i < responseMessage.length; i += chunkSize) {
const chunk = responseMessage.substring(i, i + chunkSize);
log.DEBUG("Sending message chunk:", chunk);
await message.reply(chunk);
}
} else {
message.channel.send('Sorry, I encountered an error while processing your request.');
}
}