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", "testsda@yahoo.com")
                        .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?

Oscar
  • 15
  • 7

2 Answers2

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) { }
warpri81
  • 571
  • 2
  • 6
  • `console.log("1"); if (uID === JSON.stringify(ftpr.buyers[i].id)) { console.log("2");` Is not working. It isn't coming to "2" – Oscar May 27 '18 at 20:23
  • You don't need `JSON.stringify` as that will return the id wrapped in quotes. Just compare `uID === ftpr.buyers[i].id`. – warpri81 May 27 '18 at 20:29
  • How can i add a new `"id":"new user"` so there'll be four users in the json? :D – Oscar May 27 '18 at 20:34
  • Just add it to the array as you would in javascript - `ftpr.buyers.push({id: 'new user'})` – warpri81 May 27 '18 at 20:42
  • 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. – Oscar May 27 '18 at 20:44
  • I don't think `require()` works like that. You can probably set up node to reload whenever a file is changed, or poll the file regularly for changes. You will probably get a better answer if you ask it as a separate question. – warpri81 May 27 '18 at 21:08
  • I have to wait 90 minutes before I can ask another one. – Oscar May 27 '18 at 21:12
  • lol fair enough - if you're fine with the entire node process restarting when you change the json file, check out [nodemon](https://github.com/remy/nodemon) – warpri81 May 27 '18 at 21:14
  • I have the bot opened. Then I decide "nah, this dude is not cool, im gonna remove his id" If he then tries !spotify it still gives him the token even tho i have removed him. The bot has to run 24/7 so i can't restart the program every time someone buys premium – Oscar May 27 '18 at 21:17
  • You need to periodically reload the json file or load it every time you need to compare, depending on use case. Check out [this answer](https://stackoverflow.com/a/10011078/2336208). – warpri81 May 27 '18 at 21:24
  • I have tried that but it gives me this error `TypeError: Cannot read property 'buyers' of undefined` – Oscar May 27 '18 at 21:32
  • I'd have to look at the code - post a new question :-) – warpri81 May 27 '18 at 22:46
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", "testsda@yahoo.com")
          .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;
HMR
  • 37,593
  • 24
  • 91
  • 160