From 48a576ab5605ec3ec9272809668b8d7ce91c477e Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sat, 8 Mar 2025 17:23:34 -0500 Subject: Put LLM output on text file; Some fixes in logging; New command --- bot/src/commands/fetch.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 bot/src/commands/fetch.js (limited to 'bot/src/commands/fetch.js') diff --git a/bot/src/commands/fetch.js b/bot/src/commands/fetch.js new file mode 100644 index 0000000..651fb06 --- /dev/null +++ b/bot/src/commands/fetch.js @@ -0,0 +1,34 @@ +import { AttachmentBuilder, SlashCommandBuilder } from 'discord.js'; + +export default { + data: new SlashCommandBuilder() + .setName('fetch') + .setDescription('Fetches JSON data.') + .addStringOption(option => + option + .setName('url') + .setDescription('Enter the URL you want to fetch.') + .setRequired(true)), + execute(interaction) { + const url = interaction.options.getString('url'); + fetch(url) + .then(response => response.json()) + .then(async data => { + const dataString = JSON.stringify(data, null, 2); + if (dataString.length === 0) { + return await interaction.reply('Received an empty response.'); + } + + if (dataString.length > 2000) { + const attachment = new AttachmentBuilder(Buffer.from(dataString, 'utf-8'), { name: 'messages.json' }); + return await interaction.reply({ files: [attachment] }); + } + + return await interaction.reply(`\`\`\`json\n${dataString}\n\`\`\``); + }) + .catch(async error => { + console.error(error); + return await interaction.reply(`An error occurred while fetching the URL data.\n\`\`\`js\n${error.stack}\`\`\``); + }); + } +}; -- cgit v1.2.3