如何创建” nodejs v16中的文件
我尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误很可能来自以下事实:通往文件的路径不存在。您应该尝试记录
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 withfs.access
, or create it withfs.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 replacefs.writeFileSync(...)
byawait fs.promises.writeFile(...)