簡體   English   中英

無此文件或目錄

[英]No Such File or Directory

我試圖研究可能是什么問題的其他想法,但通常歸結為不添加擴展名,或者在擴展名不存在時添加擴展名,或者主要只是拼寫。 我已經仔細檢查了所有這些,但我仍然不確定為什么會彈出這個錯誤。 我認為它與 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'}); 

有用的鏈接:

fs.readFileSync 不是文件相關的嗎? 節點.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM