Add a 1 hour timer then clear history, per user
This commit is contained in:
parent
81e55bd84b
commit
55447cb462
1
bot/.gitignore
vendored
1
bot/.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.env
|
.env
|
||||||
package-lock.json
|
package-lock.json
|
||||||
node_modules
|
node_modules
|
||||||
|
userPrivacySettings.json
|
@ -33,6 +33,25 @@ function saveUserPrivacySettings() {
|
|||||||
fs.writeFileSync(userPrivacyFilePath, JSON.stringify(userPrivacySettings, null, 2));
|
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 = [
|
const commands = [
|
||||||
new SlashCommandBuilder().setName('reset').setDescription('Reset the conversation'),
|
new SlashCommandBuilder().setName('reset').setDescription('Reset the conversation'),
|
||||||
new SlashCommandBuilder().setName('restartcore').setDescription('Restart the core service'),
|
new SlashCommandBuilder().setName('restartcore').setDescription('Restart the core service'),
|
||||||
@ -72,6 +91,12 @@ client.on('interactionCreate', async interaction => {
|
|||||||
if (!interaction.isCommand()) return;
|
if (!interaction.isCommand()) return;
|
||||||
|
|
||||||
const { commandName, options } = interaction;
|
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') {
|
if (commandName === 'reset') {
|
||||||
await resetConversation(interaction);
|
await resetConversation(interaction);
|
||||||
@ -84,13 +109,8 @@ client.on('interactionCreate', async interaction => {
|
|||||||
await togglePrivacy(interaction);
|
await togglePrivacy(interaction);
|
||||||
} else if (commandName === 'pdf') {
|
} else if (commandName === 'pdf') {
|
||||||
try {
|
try {
|
||||||
// Defer the reply to give time for PDF generation
|
|
||||||
await interaction.deferReply({ ephemeral: isEphemeral(interaction.user.id) });
|
await interaction.deferReply({ ephemeral: isEphemeral(interaction.user.id) });
|
||||||
|
|
||||||
// Generate the PDF
|
|
||||||
await generatePDF(interaction);
|
await generatePDF(interaction);
|
||||||
|
|
||||||
// After generating the PDF, you will handle the reply inside the generatePDF function
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to generate PDF:', error);
|
console.error('Failed to generate PDF:', error);
|
||||||
await interaction.editReply({ content: 'Failed to generate PDF.' });
|
await interaction.editReply({ content: 'Failed to generate PDF.' });
|
||||||
@ -152,29 +172,20 @@ async function handleUserMessage(interaction, content) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function resetConversation(interaction) {
|
async function resetConversation(interaction) {
|
||||||
try {
|
const userId = interaction.user.id;
|
||||||
const response = await axios.post(`${process.env.API_PATH}/reset-conversation`, {}, {
|
conversationHistories[userId] = []; // Reset conversation history for the user
|
||||||
headers: {
|
interaction.reply({ content: 'Conversation reset successfully.', ephemeral: isEphemeral(userId) });
|
||||||
'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) });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function restartCore(interaction) {
|
async function restartCore(interaction) {
|
||||||
|
await interaction.deferReply();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.post(`${process.env.API_PATH}/restart-core`);
|
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) {
|
} catch (error) {
|
||||||
console.log(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) });
|
await interaction.reply({ content: 'Failed to generate PDF.', ephemeral: isEphemeral(interaction.user.id) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log in the bot
|
// Log in the bot
|
||||||
client.login(process.env.THE_TOKEN_2);
|
client.login(process.env.THE_TOKEN_2);
|
||||||
|
Loading…
Reference in New Issue
Block a user