0

I tried making an evaluate command with discordjs and im trying to log the code in a file.

This is my code rn:

const { Client, MessageEmbed, MessageAttachment } = require('discord.js');
const axios = require('axios');
const fs = require('fs');
const { inspect } = require('util');
module.exports = {
  name : "evaluate",
  aliases : ['eval'],
  description: "This command allows the developer to execute a code!",
  run : async (client, messageCreate, args) => {

  if(messageCreate.author.id !== '754165961946562601') return;

  try {
    const code = args.join(" ");
    if (!code) {
       return messageCreate.channel.send("Please Provide A code to eval!");
    }
    let evaled = await eval(code);

    if (typeof evaled !== "string")
       evaled = require("util").inspect(evaled);
       const output = await evaled.substring(0,1000)
       const input = await code.substring(0,1000)
    let embed = new MessageEmbed()
       .setAuthor("Eval", messageCreate.author.avatarURL())
       .addField("Input", `\`\`\`${input}\`\`\``)
       .addField("Output", `\`\`\`${output}\`\`\``)
       .setColor("GREEN");

    messageCreate.channel.send({ embeds: [embed] })
    .then(msg => {
        setTimeout( () => {
        msg.delete()
        }, 10000
        )
        });
 } catch (err) {
    messageCreate.channel.send(`\`ERROR\` \`\`\`js\n${err}\n\`\`\``);
 }

 const data = await axios.get('http://worldtimeapi.org/api/timezone/Asia/Kolkata')
fs.writeFileSync(`./eval-logs/eval-${data.data.datetime}.txt`, "exodus")
    }



  }

Everything works, but it returns the error that the file is not found. Now I want it to create a file named eval-${data.data.datetime}.txt in eval-logs command. Can i even do it?

1 Answers1

0

The error most probably comes from the fact that the path leading to the file does not exist. You should try to log process.cwd() to check what is NodeJS current directory and verify that the folder in which you want to create the file exists with fs.access, or create it with fs.mkdir.

Note that you should avoid using synchronous APIs to access the file system as it will freeze your execution. Since you already are in an asynchronous function, I would recommend using the fs.promises module of NodeJS, and replace fs.writeFileSync(...) by await fs.promises.writeFile(...)

Ben
  • 1,331
  • 2
  • 8
  • 15