const { Client, Message } = require("discord.js"); const GuildSettings = require("../../models/GuildSettings.js"); const { getResponse, splitTextIntoParts, } = require("../../handlers/functions.js"); const conversationLogMap = new Map(); /** * * @param {Client} client * @param {Message} message */ module.exports = async (client, message) => { if (!message.guild || message.author.bot) return; let guildSettings = await GuildSettings.findOne({ guildId: message.guild.id, }); if (!guildSettings) return; if (!guildSettings.channelId) return; if (message.channel.id === guildSettings.channelId) { if (message.content.startsWith("!")) return; message.channel.sendTyping(); try { let content = message.content; let authorId = message.author.id if (message.content.includes(`<@${client.user.id}>`)) { let slice = content.replace(`<@${client.user.id}>`, ""); content = slice; } const backstory = `${guildSettings.backstory.replace( `{BotName}`, client.user.username )}`; let conversationLog = conversationLogMap.get(authorId); if (!conversationLogMap.has(authorId)) { conversationLogMap.set(authorId, backstory); conversationLog = conversationLogMap.get(authorId) } conversationLog += `\nUser: ${content}`; console.log(conversationLog) const answer = (await getResponse(conversationLog)) || "No Comment."; conversationLog += `\n Bot: ${answer}\n`; console.log(conversationLog); if (answer.length <= 2000) { message.reply(answer); } else { const parts = splitTextIntoParts(answer, 2000); for (let i = 0; i < parts.length; i++) { if (i === 0) { message.channel.send(answer); } else { await message.channel.send(answer); } } } } catch (e) { console.log("Error:", e); message.reply("An error occurred while processing the response."); } } };