The doDiscordPdf
function is an asynchronous module that generates a temporary PDF, patches a Discord interaction's image attachment with the PDF, and updates the interaction with an error message if the PDF creation fails. It utilizes various external libraries and Node.js modules, including discord api
, collect all bookmarks
, browser crawler tools
, and domain cache tools
, to retrieve a cache, perform a browser request, and interact with the file system.
npm run import -- "discord pdf converter"
const os = require('os')
const fs = require('fs')
const {updateInteraction} = importer.import("discord api")
const {savePdf} = importer.import("collect all bookmarks")
const {patchInteractionImageAttachment} = importer.import("update discord interaction with attachments")
const {doBrowserRequest} = importer.import("browser crawler tools")
const {
safeurl,
existingCache,
storeCache,
readCache,
} = importer.import("domain cache tools")
const TEMP_DIR = os.tmpdir();
let counter = 0
async function doDiscordPdf(interaction) {
// overwrite old pdfs, just for API, should transfer in memory directly, but oh well
const cache = existingCache(interaction.data.options[0].value, false)
const filename = path.join(TEMP_DIR, 'temporary_discord_pdf_' + (counter % 4) + '.pdf')
if(fs.existsSync(filename))
fs.unlinkSync(filename)
counter++
await doBrowserRequest(interaction.data.options[0].value,
readCache.bind(null, cache),
storeCache.bind(null, cache),
savePdf.bind(null, filename))
if(fs.existsSync(filename)) {
return await patchInteractionImageAttachment(interaction.data.options[0].value, fs.readFileSync(filename), interaction.token, 'application/pdf')
} else {
return await updateInteraction('Page saving failed: ' + interaction.data.options[0].value, interaction.id, interaction.token)
}
}
module.exports = {
doDiscordPdf
}
// Import required modules and tools
const os = require('os');
const fs = require('fs').promises; // Use promises for a more modern API
const { updateInteraction, patchInteractionImageAttachment } = require('discord-api');
const { savePdf } = require('collect-all-bookmarks');
const { doBrowserRequest } = require('browser-crawler-tools');
const { safeurl, existingCache, storeCache, readCache } = require('domain-cache-tools');
const path = require('path');
// Define constants
const TEMP_DIR = os.tmpdir();
// Initialize counter
let counter = 0;
/**
* Creates a PDF from a given URL and sends it as an attachment to Discord.
*
* @param {object} interaction - Discord interaction object.
* @returns {Promise
The code begins by importing various Node.js modules and functions from external libraries:
const os = require('os')
const fs = require('fs')
const {updateInteraction} = importer.import('discord api')
const {savePdf} = importer.import('collect all bookmarks')
const {patchInteractionImageAttachment} = importer.import('update discord interaction with attachments')
const {doBrowserRequest} = importer.import('browser crawler tools')
const {
safeurl,
existingCache,
storeCache,
readCache,
} = importer.import('domain cache tools')
The code sets the TEMP_DIR
variable to the system's temporary directory using the os
module:
const TEMP_DIR = os.tmpdir();
A counter
variable is initialized to keep track of the number of temporary PDFs created:
let counter = 0
doDiscordPdf
FunctionThe doDiscordPdf
function is an asynchronous function that takes an interaction
object as an argument:
async function doDiscordPdf(interaction) {
//...
}
The function does the following:
existingCache
function based on the interaction's value.counter
variable.doBrowserRequest
function, passing the interaction's value, a cache, and functions to store and read from the cache.patchInteractionImageAttachment
function.updateInteraction
function.doDiscordPdf
FunctionThe doDiscordPdf
function is exported as a module:
module.exports = {
doDiscordPdf
}
importer
object is defined elsewhere and provides a way to import functions from external libraries.path
module is used to join the TEMP_DIR
with a temporary filename, but it is not imported in this code snippet.safeurl
function is not used in this code snippet.fs
module to interact with the file system and the os
module to access the system's temporary directory.