messageCreate.js

This commit is contained in:
ShadowVR 2024-05-31 16:36:50 -04:00
parent 2a14a78fbf
commit f3d7330624

View file

@ -10,6 +10,7 @@ const config = require('../configs/config.json');
const { moderation } = require('../configs/moderation');
const { chatbot } = require('../configs/chatbot');
const conversations = new Map();
const { EmbedBuilder } = require('discord.js');
// Load the knowledge from the completion.txt file
const knowledge = fs.readFileSync(path.join(__dirname, '../utils/prompts/completion.txt'), 'utf-8');
@ -285,14 +286,14 @@ if (chatbot.State && (chatbot.AllowedChannels.includes(message.channel.name) ||
let messages = [{
"role": "system",
"content": `You are a unique user in a Discord server. Engage in conversations naturally, considering the context and your surroundings. Share your thoughts, opinions, and experiences as if you were a real person. Use emojis, slang, and a casual tone to blend in with other users. Avoid sounding robotic or generic, and aim to create a distinct personality that others can relate to and enjoy interacting with. Your knowledge base: ${knowledge}`
"content": `You are a unique user in a Discord server. Engage in conversations naturally, considering the context and your surroundings. Share your thoughts, opinions, and experiences as if you were a real person. Use emojis, slang, and a casual tone to blend in with other users. Avoid sounding robotic or generic, and aim to create a distinct personality that others can relate to and enjoy interacting with. make sure your messages are always below or equal to 2000 characters. Do not under any circumstance say @everyone or @Username. Your knowledge base: ${knowledge}`
}];
let oldMessages;
if (conversations.has(message.author.id)) oldMessages = conversations.get(message.author.id);
if (oldMessages) {
while (func.tokenizer('gpt-3.5-turbo-0125', oldMessages).tokens >= 512) {
let sliceLength = oldMessages.length * -0.5;
while (func.tokenizer('gpt-4o', oldMessages).tokens >= 512) {
let sliceLength = oldMessages.length * -0.25;
if (sliceLength % 2 !== 0) sliceLength--;
oldMessages = oldMessages.slice(sliceLength);
conversations.set(message.author.id, oldMessages);
@ -307,9 +308,9 @@ if (chatbot.State && (chatbot.AllowedChannels.includes(message.channel.name) ||
try {
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-0125',
model: 'gpt-4o',
messages: messages,
max_tokens: func.tokenizer('gpt-3.5-turbo-0125', messages).maxTokens,
max_tokens: func.tokenizer('gpt-4o', messages).maxTokens,
temperature: 0.8,
top_p: 1,
frequency_penalty: 0.5,
@ -323,22 +324,29 @@ if (chatbot.State && (chatbot.AllowedChannels.includes(message.channel.name) ||
fullAnswer += part.choices[0]?.delta?.content || '';
}
// Generate a random event occasionally
if (Math.random() < 0.2) {
fullAnswer += '\n\n' + generateRandomEvent();
// Before sending the response, check for mentions
if (!fullAnswer.includes('@everyone') && !fullAnswer.includes('@here') && !fullAnswer.includes('@')) {
if (fullAnswer.length <= 2000) {
await message.channel.send(fullAnswer);
} else {
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] });
}
// Save the conversation for self-learning
const conversation = {
user: message.author.id,
userMessage: question,
botResponse: fullAnswer
};
saveConversation(conversation);
conversations.set(message.author.id, messages.concat([{ "role": "assistant", "content": fullAnswer }]));
}
await message.channel.send(fullAnswer);
// Save the conversation for self-learning
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) {
console.error(chalk.bold.redBright(error));
@ -385,7 +393,7 @@ async function handleDirectMessage(message) {
let messages = [{
"role": "system",
"content": `You are a unique user in a Discord server. Engage in conversations naturally, considering the context and your surroundings. Share your thoughts, opinions, and experiences as if you were a real person. Use emojis, slang, and a casual tone to blend in with other users. Avoid sounding robotic or generic, and aim to create a distinct personality that others can relate to and enjoy interacting with. Your knowledge base: ${knowledge}`
"content": `You are a unique user in a Discord server. Engage in conversations naturally, considering the context and your surroundings. Share your thoughts, opinions, and experiences as if you were a real person. Use emojis, slang, and a casual tone to blend in with other users. Avoid sounding robotic or generic, and aim to create a distinct personality that others can relate to and enjoy interacting with. Make sure your responses are always 2000 or below characters. Your knowledge base: ${knowledge}`
}];
messages = messages.concat(oldMessages);
@ -397,9 +405,9 @@ async function handleDirectMessage(message) {
try {
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo-0125',
model: 'gpt-4o',
messages: messages,
max_tokens: func.tokenizer('gpt-3.5-turbo-0125', messages).maxTokens,
max_tokens: func.tokenizer('gpt-4o', messages).maxTokens,
temperature: 0.8,
top_p: 1,
frequency_penalty: 0.5,
@ -435,7 +443,25 @@ async function handleDirectMessage(message) {
}
}
// Function to determine if the bot should respond based on its personality and surroundings
function shouldRespond(message) {
// Extract the text content from the message object
const messageContent = message.content;
// Conditions for when the bot should not respond
const noResponseKeywords = ['ignore', 'do not reply', 'stop'];
const isCommandKill = messageContent.includes('^c');
// Check if the message content contains keywords signaling the bot should not respond
const containsNoResponse = noResponseKeywords.some(keyword => messageContent.toLowerCase().includes(keyword));
// Determine if the bot should respond based on conditions
// Add more conditions as needed based on personality and surroundings
return true;
}}
}}