0

I tried to look into other ideas of what might be the issue, but it usually comes down to not adding in the extension, or adding the extension when it isn't there, or mainly just spelling. I have double checked all of that and I'm still not sure why this error is popping up. I assume it has to do with the readFileSync but I'm not even sure what else I would put in place of that.

Basically what I'm trying to do is store the amount of candy users get in a json file. No I don't want to use a database as this is only a temporary thing for Halloween. The code I found to store the info was from a few years ago so I know the code is outdated, but I've looked and I'm still unsure on how it's supposed to be.

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);
    },
};

enter image description here

enter image description here

QuazArxx
  • 150
  • 2
  • 15
  • 2
    please show full error and your directory structure – Lawrence Cherone Oct 14 '20 at 15:21
  • I added both the error and the directory structure as images to the bottom of the original post. – QuazArxx Oct 14 '20 at 15:31
  • remove `DiscordBot/` from the path, candyheld.json is in the same dir as index.js – Lawrence Cherone Oct 14 '20 at 15:32
  • The command is in the directory DiscordBot/commands/Halloween/trickortreat.js I have also tried to use '../../../candyheld.json' as the directory name. – QuazArxx Oct 14 '20 at 15:41
  • 1
    @QuazArxx Try ```fs.readFileSync('./DiscordBot/candyheld.json', {encoding:'utf8'}); ``` and helpful link https://stackoverflow.com/questions/44600943/fs-readfilesync-is-not-file-relative-node-js – Dolly Oct 14 '20 at 15:55
  • @Dolly while that didn't work, I set it to './candyheld.json' which did work. I guess it reads from wherever the index file is located but I didn't realize I needed that './' in there. Now that that's working the real issue I saw coming came up. 'guild' is not defined. I tried putting 'message.guild.members' but then I got 'message.guild.members.random()' is not a function. – QuazArxx Oct 14 '20 at 16:21
  • Ideally, you should always use `path.join([__dirname, 'DiscordBot', 'candyheld.json'])`, then it will yield a proper path which you can deduce is wrong.. the second question is `await message.guild.members.fetch()` then you can use as an array – Lawrence Cherone Oct 14 '20 at 17:00

1 Answers1

0

Use "./" in front:

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

Helpful link:

fs.readFileSync is Not File Relative? Node.js

Dolly
  • 2,213
  • 16
  • 38