Add PDF
This commit is contained in:
parent
a58353b9c3
commit
356d128fce
@ -1,27 +1,24 @@
|
|||||||
const { Client, GatewayIntentBits, REST, Routes, EmbedBuilder, SlashCommandBuilder } = require('discord.js');
|
const { Client, GatewayIntentBits, REST, Routes, EmbedBuilder, SlashCommandBuilder, AttachmentBuilder } = require('discord.js');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const he = require('he');
|
const he = require('he');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const PDFDocument = require('pdfkit'); // Import PDFKit
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const { userResetMessages } = require('./assets/messages.js');
|
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [GatewayIntentBits.Guilds]
|
intents: [GatewayIntentBits.Guilds]
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load or initialize the user privacy settings
|
|
||||||
const userPrivacyFilePath = './userPrivacySettings.json';
|
const userPrivacyFilePath = './userPrivacySettings.json';
|
||||||
let userPrivacySettings = {};
|
let userPrivacySettings = {};
|
||||||
if (fs.existsSync(userPrivacyFilePath)) {
|
if (fs.existsSync(userPrivacyFilePath)) {
|
||||||
userPrivacySettings = JSON.parse(fs.readFileSync(userPrivacyFilePath));
|
userPrivacySettings = JSON.parse(fs.readFileSync(userPrivacyFilePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the user privacy settings
|
|
||||||
function saveUserPrivacySettings() {
|
function saveUserPrivacySettings() {
|
||||||
fs.writeFileSync(userPrivacyFilePath, JSON.stringify(userPrivacySettings, null, 2));
|
fs.writeFileSync(userPrivacyFilePath, JSON.stringify(userPrivacySettings, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the 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'),
|
||||||
@ -30,13 +27,14 @@ const commands = [
|
|||||||
option.setName('message')
|
option.setName('message')
|
||||||
.setDescription('Message to send')
|
.setDescription('Message to send')
|
||||||
.setRequired(true)),
|
.setRequired(true)),
|
||||||
new SlashCommandBuilder().setName('privacy').setDescription('Toggle between ephemeral and standard responses')
|
new SlashCommandBuilder().setName('privacy').setDescription('Toggle between ephemeral and standard responses'),
|
||||||
|
new SlashCommandBuilder().setName('pdf').setDescription('Download conversation history as a PDF')
|
||||||
].map(command => {
|
].map(command => {
|
||||||
const commandJSON = command.toJSON();
|
const commandJSON = command.toJSON();
|
||||||
|
|
||||||
const extras = {
|
const extras = {
|
||||||
"integration_types": [0, 1], // 0 for guild, 1 for user
|
"integration_types": [0, 1],
|
||||||
"contexts": [0, 1, 2] // 0 for guild, 1 for app DMs, 2 for GDMs and other DMs
|
"contexts": [0, 1, 2]
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.keys(extras).forEach(key => commandJSON[key] = extras[key]);
|
Object.keys(extras).forEach(key => commandJSON[key] = extras[key]);
|
||||||
@ -44,7 +42,6 @@ const commands = [
|
|||||||
return commandJSON;
|
return commandJSON;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register commands with Discord
|
|
||||||
const rest = new REST({ version: '10' }).setToken(process.env.THE_TOKEN_2);
|
const rest = new REST({ version: '10' }).setToken(process.env.THE_TOKEN_2);
|
||||||
|
|
||||||
client.once('ready', async () => {
|
client.once('ready', async () => {
|
||||||
@ -63,7 +60,7 @@ client.on('interactionCreate', async interaction => {
|
|||||||
const { commandName, options } = interaction;
|
const { commandName, options } = interaction;
|
||||||
|
|
||||||
if (commandName === 'reset') {
|
if (commandName === 'reset') {
|
||||||
return await resetConversation(interaction);
|
await resetConversation(interaction);
|
||||||
} else if (commandName === 'restartcore') {
|
} else if (commandName === 'restartcore') {
|
||||||
await restartCore(interaction);
|
await restartCore(interaction);
|
||||||
} else if (commandName === 'chat') {
|
} else if (commandName === 'chat') {
|
||||||
@ -71,6 +68,8 @@ client.on('interactionCreate', async interaction => {
|
|||||||
await handleUserMessage(interaction, content);
|
await handleUserMessage(interaction, content);
|
||||||
} else if (commandName === 'privacy') {
|
} else if (commandName === 'privacy') {
|
||||||
await togglePrivacy(interaction);
|
await togglePrivacy(interaction);
|
||||||
|
} else if (commandName === 'pdf') {
|
||||||
|
await generatePDF(interaction);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -203,5 +202,60 @@ async function sendLongMessage(interaction, responseText) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate a PDF of the conversation history
|
||||||
|
async function generatePDF(interaction) {
|
||||||
|
try {
|
||||||
|
// Fetch the conversation history from the API
|
||||||
|
const response = await axios.get(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/api/v1/conversation-history`, {
|
||||||
|
headers: {
|
||||||
|
'x-forwarded-for-id': interaction.user.id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const conversationHistory = response.data;
|
||||||
|
|
||||||
|
// Filter out system messages
|
||||||
|
const filteredHistory = conversationHistory.filter(message => message.role !== 'system');
|
||||||
|
|
||||||
|
// Create a PDF document
|
||||||
|
const doc = new PDFDocument();
|
||||||
|
const pdfPath = `./conversation_${interaction.user.id}.pdf`;
|
||||||
|
const writeStream = fs.createWriteStream(pdfPath);
|
||||||
|
|
||||||
|
// Pipe the document to a file
|
||||||
|
doc.pipe(writeStream);
|
||||||
|
|
||||||
|
// Add conversation history to the PDF
|
||||||
|
doc.fontSize(14).text('Conversation History', { align: 'center' });
|
||||||
|
doc.moveDown();
|
||||||
|
|
||||||
|
filteredHistory.forEach((message, index) => {
|
||||||
|
const role = message.role.charAt(0).toUpperCase() + message.role.slice(1);
|
||||||
|
const content = he.decode(message.content); // Decode any HTML entities
|
||||||
|
doc.fontSize(12).text(`${role}: ${content}`, { align: 'left' });
|
||||||
|
doc.moveDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.end();
|
||||||
|
|
||||||
|
// Wait for the PDF to finish writing
|
||||||
|
writeStream.on('finish', async () => {
|
||||||
|
const attachment = new AttachmentBuilder(pdfPath);
|
||||||
|
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Here is your conversation history in PDF format:',
|
||||||
|
files: [attachment],
|
||||||
|
ephemeral: isEphemeral(interaction.user.id)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Clean up the PDF file after sending it
|
||||||
|
fs.unlinkSync(pdfPath);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to generate PDF:', error);
|
||||||
|
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