parent
c75a820585
commit
4c57a66a97
1 changed files with 61 additions and 39 deletions
|
@ -11,6 +11,7 @@ const { moderation } = require('../configs/moderation');
|
||||||
const { chatbot } = require('../configs/chatbot');
|
const { chatbot } = require('../configs/chatbot');
|
||||||
const conversations = new Map();
|
const conversations = new Map();
|
||||||
const { EmbedBuilder } = require('discord.js');
|
const { EmbedBuilder } = require('discord.js');
|
||||||
|
const { exec } = require('child_process');
|
||||||
|
|
||||||
// Load the knowledge from the completion.txt file
|
// Load the knowledge from the completion.txt file
|
||||||
const knowledge = fs.readFileSync(path.join(__dirname, '../utils/prompts/completion.txt'), 'utf-8');
|
const knowledge = fs.readFileSync(path.join(__dirname, '../utils/prompts/completion.txt'), 'utf-8');
|
||||||
|
@ -324,58 +325,79 @@ if (chatbot.State && (chatbot.AllowedChannels.includes(message.channel.name) ||
|
||||||
fullAnswer += part.choices[0]?.delta?.content || '';
|
fullAnswer += part.choices[0]?.delta?.content || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fullAnswer.includes('@everyone') && !fullAnswer.includes('@here') && !fullAnswer.includes('@')) {
|
const fs = require('fs');
|
||||||
|
|
||||||
|
// Function to restart the bot
|
||||||
|
function restartBot() {
|
||||||
|
console.log("Restarting bot...");
|
||||||
|
process.exit(0); // Exits the current process, assuming your process manager restarts the bot
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to restart the bot
|
||||||
|
function restartBot() {
|
||||||
|
console.log("Restarting bot...");
|
||||||
|
process.exit(0); // Exits the current process, assuming your process manager restarts the bot
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to save conversation for self-learning
|
||||||
|
function saveConversation(conversation) {
|
||||||
|
// Implement the logic to save the conversation
|
||||||
|
// For example, you can write it to a file or a database
|
||||||
|
const data = JSON.stringify(conversation, null, 2);
|
||||||
|
fs.appendFileSync('conversations.json', data + ',\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Before sending the response, check for mentions
|
||||||
|
if (!fullAnswer.includes('@everyone') && !fullAnswer.includes('@here') && !fullAnswer.includes('@')) {
|
||||||
if (fullAnswer.length <= 2000) {
|
if (fullAnswer.length <= 2000) {
|
||||||
await message.channel.send(fullAnswer);
|
await message.channel.send(fullAnswer);
|
||||||
|
} else if (fullAnswer.length <= 4096) {
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle('Response from GPT-4o')
|
||||||
|
.setDescription(fullAnswer.substring(0, 4096)) // Embed description has a 4096 character limit
|
||||||
|
.setColor('#0099ff');
|
||||||
|
|
||||||
|
await message.channel.send({ embeds: [embed] });
|
||||||
} else {
|
} else {
|
||||||
// Function to split the fullAnswer into chunks of 4096 characters
|
// Save the full answer to a .txt file
|
||||||
const splitIntoChunks = (str, chunkSize) => {
|
fs.writeFileSync('response.txt', fullAnswer);
|
||||||
const chunks = [];
|
|
||||||
for (let i = 0; i < str.length; i += chunkSize) {
|
|
||||||
chunks.push(str.slice(i, i + chunkSize));
|
|
||||||
}
|
|
||||||
return chunks;
|
|
||||||
};
|
|
||||||
|
|
||||||
const chunks = splitIntoChunks(fullAnswer, 4096);
|
// Send the .txt file
|
||||||
|
await message.channel.send({
|
||||||
|
files: [{
|
||||||
|
attachment: 'response.txt',
|
||||||
|
name: 'response.txt'
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
// Send each chunk as an embed
|
// Restart the bot
|
||||||
for (const chunk of chunks) {
|
restartBot();
|
||||||
const embed = new EmbedBuilder()
|
|
||||||
.setTitle('Response from GPT-4o')
|
|
||||||
.setDescription(chunk)
|
|
||||||
.setColor('#0099ff');
|
|
||||||
|
|
||||||
await message.channel.send({ embeds: [embed] });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit the process to trigger a restart
|
|
||||||
process.exit(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the conversation for self-learning
|
||||||
|
const conversation = {
|
||||||
|
user: message.author.id,
|
||||||
|
userMessage: question,
|
||||||
|
botResponse: fullAnswer
|
||||||
|
};
|
||||||
|
saveConversation(conversation);
|
||||||
|
|
||||||
// Save the conversation for self-learning
|
conversations.set(message.author.id, messages.concat([{ "role": "assistant", "content": fullAnswer }]));
|
||||||
const conversation = {
|
|
||||||
user: message.author.id,
|
|
||||||
userMessage: question,
|
|
||||||
botResponse: fullAnswer
|
|
||||||
};
|
|
||||||
saveConversation(conversation);
|
|
||||||
|
|
||||||
conversations.set(message.author.id, messages.concat([{ "role": "assistant", "content": fullAnswer }]));
|
} catch (error) {
|
||||||
} catch (error) {
|
console.error(chalk.bold.redBright(error));
|
||||||
console.error(chalk.bold.redBright(error));
|
|
||||||
|
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
await message.reply(error.response.error.message.substring(0, 2000));
|
await message.reply(error.response.error.message.substring(0, 2000));
|
||||||
} else if (error.message) {
|
} else if (error.message) {
|
||||||
await message.reply(error.message.substring(0, 2000));
|
await message.reply(error.message.substring(0, 2000));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Command Handler
|
// Command Handler
|
||||||
if (message.content.toLowerCase().startsWith(config.Prefix)) {
|
if (message.content.toLowerCase().startsWith(config.Prefix)) {
|
||||||
|
|
Loading…
Reference in a new issue