Add a 1 hour timer then clear history, per user

This commit is contained in:
Raven 2024-10-04 04:13:32 -04:00
parent 81e55bd84b
commit 55447cb462
2 changed files with 35 additions and 22 deletions

1
bot/.gitignore vendored
View File

@ -1,3 +1,4 @@
.env
package-lock.json
node_modules
userPrivacySettings.json

View File

@ -33,6 +33,25 @@ function saveUserPrivacySettings() {
fs.writeFileSync(userPrivacyFilePath, JSON.stringify(userPrivacySettings, null, 2));
}
// Store the last interaction time and conversation history for each user
let userLastInteraction = {};
let conversationHistories = {};
// Function to reset conversation history if more than one hour has passed
function resetConversationIfExpired(userId) {
const now = Date.now();
if (userLastInteraction[userId] && now - userLastInteraction[userId] >= 3600000) { // 1 hour in milliseconds
conversationHistories[userId] = []; // Reset conversation history
console.log(`Conversation history reset for user ${userId} due to inactivity.`);
}
}
// Function to update last interaction time for the user
function updateUserInteractionTime(userId) {
userLastInteraction[userId] = Date.now();
}
// Register commands
const commands = [
new SlashCommandBuilder().setName('reset').setDescription('Reset the conversation'),
new SlashCommandBuilder().setName('restartcore').setDescription('Restart the core service'),
@ -72,6 +91,12 @@ client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
const userId = interaction.user.id;
// Update the last interaction time for the user
updateUserInteractionTime(userId);
// Reset the conversation history if an hour has passed
resetConversationIfExpired(userId);
if (commandName === 'reset') {
await resetConversation(interaction);
@ -84,13 +109,8 @@ client.on('interactionCreate', async interaction => {
await togglePrivacy(interaction);
} else if (commandName === 'pdf') {
try {
// Defer the reply to give time for PDF generation
await interaction.deferReply({ ephemeral: isEphemeral(interaction.user.id) });
// Generate the PDF
await generatePDF(interaction);
// After generating the PDF, you will handle the reply inside the generatePDF function
} catch (error) {
console.error('Failed to generate PDF:', error);
await interaction.editReply({ content: 'Failed to generate PDF.' });
@ -152,29 +172,20 @@ async function handleUserMessage(interaction, content) {
}
async function resetConversation(interaction) {
try {
const response = await axios.post(`${process.env.API_PATH}/reset-conversation`, {}, {
headers: {
'Content-Type': 'application/json',
'x-forwarded-for-id': interaction.user.id,
'x-forwarded-for-name': interaction.user.username
}
});
console.log(response.status);
interaction.reply({ content: 'Conversation reset successfully.', ephemeral: isEphemeral(interaction.user.id) });
} catch (error) {
console.log(error);
interaction.reply({ content: 'Failed to reset the conversation.', ephemeral: isEphemeral(interaction.user.id) });
}
const userId = interaction.user.id;
conversationHistories[userId] = []; // Reset conversation history for the user
interaction.reply({ content: 'Conversation reset successfully.', ephemeral: isEphemeral(userId) });
}
async function restartCore(interaction) {
await interaction.deferReply();
try {
await axios.post(`${process.env.API_PATH}/restart-core`);
interaction.reply({ content: 'Core service restarted successfully.', ephemeral: isEphemeral(interaction.user.id) });
interaction.editReply({ content: 'Core service restarted successfully.', ephemeral: isEphemeral(interaction.user.id) });
} catch (error) {
console.log(error);
interaction.reply({ content: 'Failed to restart the core.', ephemeral: isEphemeral(interaction.user.id) });
interaction.editReply({ content: 'Failed to restart the core.', ephemeral: isEphemeral(interaction.user.id) });
}
}
@ -291,5 +302,6 @@ async function generatePDF(interaction) {
await interaction.reply({ content: 'Failed to generate PDF.', ephemeral: isEphemeral(interaction.user.id) });
}
}
// Log in the bot
client.login(process.env.THE_TOKEN_2);