0

I have a JSON File: (invitelist.json)

    {
    }

Every time the "guildMemberAdd" event gets fired off, an invite is created:

bot.channels.get('642609367195189268').createInvite({
  temporary: false,
  maxAge: 0,
  maxUses: 0,
  unique: true
},`This is ${member}'s invite link.`).then(invite => {
  bot.channels.get('642609367195189268').send(invite.code);
});

How do I append to the JSON File?:

{
  "invite": {
    "code": "",
    "timecreated": "",
    "author": ""
  }
}

When a new invite is created, it'll add another invite object:

{
  "invite-1": {
    "code": "",
    "timecreated": "",
    "author": ""
  },
  "invite-2": {
    "code": "",
    "timecreated": "",
    "author": ""
  }
}

The address to the invitelist.json is simply invitelist.json.

If you could please provide me some code and most importantly what the result of your solution to this question will look like, that'd be great, oh and explanation of how your solutions works would be awesome too! Thanks

If you need more information please just kindly ask in the comments below :D

0

1 Answer 1

0

Javascript Append JSON Objects

So, after 14 hours of no response, 3 hours of sleep, 11 hours or searching on google and a few hours of messing around with code. I finally found a question that answers my question for me. This is a snippet of my end result:

    var inviteAmount = 0;
bot.on('guildMemberAdd', async member => {
    inviteAmount += 1;

    var data = [];
    var i;
    var ia = inviteAmount + 1;

    function Client(date, contact) {
          this.date = date
          this.contact = contact
    }
    var clients = new Array();
    for (i = 1; i < ia; i++) {
        clients.push(new Client(
          "2018-08-0" + i,
          i
        ));
    }
    for (i = 0; i < clients.length; i++) {
        var dict = {}
        dict['Date'] = clients[i].date
        dict['Contact'] = clients[i].contact
        data[i] = dict
    }

    console.log(data);
    console.log("test");
    bot.fetchUser(member.id).then((user) => {
        user.send(JSON.stringify(data));
    });

Result:

[{"Date":"2018-08-01","Contact":1},{"Date":"2018-08-02","Contact":2}]

I used this question: Javascript Append JSON Objects

Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to edit this answer to make more sense, have an explanation and actually fit in the OP's example code in a little bit.

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.