1

Im working on a Discord bot with commands for premium members.

buyers.json:

buyers.json:

{
  "buyers": [
    {
      "id": "331499609509724162"
    },
    {
      "id": "181336616164392960"
    },
    {
      "id": "266389854122672128"
    }
  ]
}

Snippet of the code:

case "spotify":
            var uID = message.author.id;
            for (let i = 0; i < ftpr.buyers.length; i++) {
                if (uID === ftpr.buyers[i]) {
                    var _embed = new Discord.RichEmbed()
                        .setTitle("Spotify")
                        .addField("Username", "[email protected]")
                        .addField("Password", "ithastobe8")
                        .setColor(0x00FFFF)
                        .setFooter("Sincerely, LajgaardMoneyService")
                        .setThumbnail(message.author.avatarURL);
                    message.author.send(_embed);
                    console.log(message.author + " Just used the command !spotify")
                }
                
            }
            
            break;

Everything works except the for loop and if statement. Its like it aint getting the id's.

var fptr = require("./buyers.json");

EDIT: my json file looks like this:

{
  "buyers": [
    {
      "id": "331499609509724162"
    },
    {
      "id": "181336616164392960"
    },
    {
      "id": "266389854122672128"
    }
  ]
}

It has some id's from people. In my app.js var file = require("./file.json"); is used to import. Lets say that I change one of the ID's then I have to reload the app.js to update it. If I don't my checking mechanism isn't using the updated version of file.json How should I go about updating the file every time that a function is being used?

5
  • Are you actually exporting the object, or just declaring it? Any console errors? Commented May 27, 2018 at 20:13
  • No errors at all. The ID's in the JSON file are the same as "message.author.id" but it isn't looping through the json file. Commented May 27, 2018 at 20:15
  • 1
    If the for loop isn't iterating at all, then ftpr.buyers.length is 0 or undefined - add log statements around those lines to check what it really is Commented May 27, 2018 at 20:16
  • Note that if you actually have a JSON, you have to parse it first. Commented May 27, 2018 at 20:16
  • shouldn't it be uID === ftpr.buyers[i].id instead of uID === ftpr.buyers[i]? Commented May 27, 2018 at 20:17

2 Answers 2

1

You are comparing the uID, which I assume is a string, to each object in the array (which contains a string).

Try comparing to the id attribute of the buyers objects:

if (uID === ftpr.buyers[i].id) { }
Sign up to request clarification or add additional context in comments.

12 Comments

console.log("1"); if (uID === JSON.stringify(ftpr.buyers[i].id)) { console.log("2"); Is not working. It isn't coming to "2"
You don't need JSON.stringify as that will return the id wrapped in quotes. Just compare uID === ftpr.buyers[i].id.
How can i add a new "id":"new user" so there'll be four users in the json? :D
Just add it to the array as you would in javascript - ftpr.buyers.push({id: 'new user'})
Thanks, but when I have to reload the app.js program in order to update the json file. How can I live update? I have tried ftpr = require("./buyers.json"); In hope that it would update. But that didn't work.
|
0

Make sure that message.author.id and ftpr.buyers id's have the same type, the ftpr.buyers is an array of objects containing id of string. You should console.log the type of message.author.id by doing console.log(typeof message.author.id,message.author.id)

You could simplify the code by having an array of strings containing the id's before your switch case:

const ids = ftpr.buyers.map(b = b.id);//this is now ["331499609509724162",...]

  case "spotify":
      if (ids.includes(message.author.id)) {
        var _embed = new Discord.RichEmbed()
          .setTitle("Spotify")
          .addField("Username", "[email protected]")
          .addField("Password", "ithastobe8")
          .setColor(0x00FFFF)
          .setFooter("Sincerely, LajgaardMoneyService")
          .setThumbnail(message.author.avatarURL);
        message.author.send(_embed);
        console.log(message.author + " Just used the command !spotify")
      }

    break;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.