如何创建” nodejs v16中的文件

发布于 2025-01-21 13:20:34 字数 1671 浏览 0 评论 0原文

我尝试使用Discordjs进行评估命令,并试图在文件中记录代码。

这是我的代码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")
    }



  }

一切都起作用,但是它返回找不到文件的错误。现在,我希望它能创建一个名为eval的文件 - $ {data.data.dateTime} .txt在eval-logs命令中。我什至可以做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-01-28 13:20:34

错误很可能来自以下事实:通往文件的路径不存在。您应该尝试记录process.cwd()以检查nodejs current current directory,并验证要使用fs.access,或使用fs.mkdir创建它。

请注意,您应该避免使用同步API访问文件系统,因为它会冻结您的执行。由于您已经处于异步功能,因此我建议使用nodejs的fs.promises模块,然后替换fs.writefilesync(...) 等待fs.promises.writefile(...)

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(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文