58 lines
1.6 KiB
JavaScript
58 lines
1.6 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;
|
|
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.",
|
|
);
|
|
}
|
|
};
|