From 1f70cb9b1cf25d353012510689972728b10789e2 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Wed, 5 Apr 2023 08:30:58 -0500 Subject: [PATCH] Write content to file if string is too big to send --- commands/addPhrase.cjs | 2 +- commands/drawTriangle.cjs | 32 ++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/commands/addPhrase.cjs b/commands/addPhrase.cjs index 6e4e0c6..5bd9a1e 100644 --- a/commands/addPhrase.cjs +++ b/commands/addPhrase.cjs @@ -1,7 +1,7 @@ const { SlashCommandBuilder } = require('discord.js'); var abbreviationKey = require("../abbreviation_key.json"); const fs = require('node:fs'); -var path = require('node:path');; +var path = require('node:path'); //console.log(abbreviationKey); module.exports = { diff --git a/commands/drawTriangle.cjs b/commands/drawTriangle.cjs index 95fc7ff..c177188 100644 --- a/commands/drawTriangle.cjs +++ b/commands/drawTriangle.cjs @@ -1,4 +1,6 @@ -const { SlashCommandBuilder, Discord, codeBlock } = require('discord.js'); +const { SlashCommandBuilder, Discord, codeBlock, EmbedBuilder } = require('discord.js'); +const fs = require('fs'); +var path = require('node:path'); //const { writeFile } = require('../main.cjs'); module.exports = { @@ -37,19 +39,33 @@ module.exports = { output = design4(inputString); } console.log(codeBlock("", output)); - writeFile(); let testLength = "\n" + 'output length was ' + (output.length) + " characters."; if (output.length + testLength.length > 2000) { - await interaction.reply('Error: String is too big. Resultant output would be too big to send to Discord. (the output length was ' + (output.length + testLength.length) + " characters.)"); - return; + //await interaction.reply('Error: String is too big. Resultant output would be too big to send to Discord. (the output length was ' + (output.length + testLength.length) + " characters.)"); + let replyString = "output length was " + (output.length) + " characters."; + let filePath = writeFile(output); + await interaction.reply({content:replyString, files: [filePath] }); + fs.unlinkSync(filePath); // Delete file once we're done with it + } else { + await interaction.reply(codeBlock("", output) + testLength); } - await interaction.reply(codeBlock("", output) + testLength); console.log("User " + interaction.user.tag + " ran /drawtriangle"); }, }; -function writeFile() { - console.log("Write file was called") +function writeFile(content) { + //console.log(content); + console.log("Attempting to write string to file..."); + let filename = Date.now()+'.txt'; + console.log(filename); + var filePath = path.join(__dirname, '..', filename); + console.log(filePath); + fs.writeFile(filePath, content, err => { + if (err) { + console.error(err); + } + }); + return filePath; } function design1(input) { @@ -153,7 +169,7 @@ function design4(input) { } // for horizontal movement for (let i = 0; i <= 2 * radius; i++) { - if (i != mid && j != 0) { + if (i != mid) { // for vertical movement for (let j = 0; j <= 2 * radius; j++) {