首页 > 解决方案 > 没有这样的文件或目录

问题描述

我试图研究可能是什么问题的其他想法,但通常归结为不添加扩展名,或者在不存在时添加扩展名,或者主要只是拼写。我已经仔细检查了所有这些,但我仍然不确定为什么会弹出这个错误。我认为它与 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);
    },
};

在此处输入图像描述

在此处输入图像描述

标签: javascriptdiscorddiscord.js

解决方案


在前面使用“./”:

fs.readFileSync('./DiscordBot/candyheld.json',  {encoding:'utf8'}); 

有用的链接:

fs.readFileSync 不是文件相关的?节点.js


推荐阅读