javascript - 没有这样的文件或目录
问题描述
我试图研究可能是什么问题的其他想法,但通常归结为不添加扩展名,或者在不存在时添加扩展名,或者主要只是拼写。我已经仔细检查了所有这些,但我仍然不确定为什么会弹出这个错误。我认为它与 readFileSync 有关,但我什至不确定我还会用什么来代替它。
基本上我想要做的是将用户获得的糖果数量存储在一个 json 文件中。不,我不想使用数据库,因为这只是万圣节的临时事情。我发现存储信息的代码是几年前的,所以我知道代码已经过时了,但我已经看过了,但我仍然不确定它应该是怎样的。
const fs = require('fs');
const candyAmount = JSON.parse(fs.readFileSync('DiscordBot/candyheld.json', 'utf8'));
module.exports = {
name: 'trickortreat',
description: 'Special Halloween command',
execute(message, args) {
if (!candyAmount[message.author.id]) {
candyAmount[message.author.id] = {
candyStored: 5
}
return message.channel.send('For starting this event, you have received 5 pieces of candy!')
}
// Stores a random number of candy from 1-3
let candy = Math.floor(Math.random() * 3);
// Sets the possible messages to be received
let trickortreatmessage = [
'The scarecrow standing behind you jumps out at you and makes you drop all your candy!',
`${guild.members.random()} was nice enough to give you Candy! You got ${candy} pieces of candy!`,
`Oh no you asked ${guild.members.random()} for Candy and they decided to egg you instead!`
]
// Store one of the random messages
const trickortreat = Math.floor(Math.random() * trickortreatmessage);
if (trickortreat == trickortreatmessage[0]) {
candyAmount[message.author.id].candyStored - candyStored;
} else if (trickortreat == trickortreatmessage[1]) {
candyAmount[message.author.id].candyStored + candy;
}
fs.writeFile('DiscordBot/candyheld.json', JSON.stringify(candyAmount), err => {
if (err) console.error(err);
});
message.channel.send(trickortreat);
},
};
解决方案
在前面使用“./”:
fs.readFileSync('./DiscordBot/candyheld.json', {encoding:'utf8'});
有用的链接:
推荐阅读
- python - 获取模块的导入名称
- javascript - 在 VueJS 中动态添加选择字段和验证
- algorithm - 给定一个公式,求解一个未知变量
- javascript - 在状态更改时更改 TileLayer 的瓦片
- javascript - DOM 使用 addEventListener 改变字体颜色
- javascript - 查找字符串数组中匹配的多个值的索引
- c# - 未检测到 Dll(但作为依赖项和导入路径 -VSCode 存在)
- macos - JupyterLab - 如何添加 Command(⌘) + Enter 以在 macOS 中运行单元格?(怎么叫⌘?)
- css - Rubymine css代码完成不起作用
- shell - 如何使用脚本删除文件中重复的填充字符?