1

I've been working on a project for a friend's discord server, where I want to keep sort of a 'database' on our members, like a warning (or strike) count, their age (because we've been having issues with underage members) and some notes. I've been using tutorials as I'm pretty new to javascript, and really new to Discord.js, so, I'm not really sure on what I'm doing yet.

(to clarify, I'm not getting any errors, it's just not updating to the new file)

Here are the resources I've used:

https://www.youtube.com/watch?v=JB1rWJRafRA

https://discordjs.guide/popular-topics/collectors.html#message-collectors

https://www.youtube.com/watch?v=J2bKyv5pp-M

And my code: (sorry in advance, it's really messy)

const Discord = require('discord.js');
const fs = require('fs');
const jsonfile = require('jsonfile');
const Client = new Discord.Client();

var data = {}; //start of member activeness tracker
if (fs.existsSync('data.json')) {
    data = jsonfile.readFileSync('data.json');
}

console.log("Bot Starting...");

Client.on("ready", () => console.log('ServerMgr is ready!  Now managing servers!'));  //bot init complete

Client.on("message", (message) =>  {

    if (message.guild.id in data === false) {data[message.guild.id] = {} } //if new guild

    const guildData = data[message.guild.id];

    if (message.author.id in guildData === false) {
        guildData[message.author.id] = {
            username: message.author.username,
            message_count: 0,
            last_message: 0,
            age: 0,
            times_left: 0,
            strikes: 0,
            notes: ""
         }
    }

    const userData = guildData[message.author.id];
    userData.message_count ++
    userData.last_message = Date.now();

    jsonfile.writeFileSync('data.json', data)

    const parts = message.content.split(' ') 
    if(parts[0] === ';ping') {message.reply('```SeverMgr is online!\nCurrent Ping: ' + (Math.abs(Date.now() - message.createdTimestamp))/ 100 + " ms```")}
    if(parts[0] === ';read') {
        if (message.mentions.members.first()) {
            var user = message.mentions.users.first()
            const id = user.id
            const userData = guildData[id]
            const lastMessage = new Date(userData.last_message)
            message.channel.send("Getting data for " + user.username + "#" + user.discriminator + "...")
            message.channel.send("Sent " + userData.message_count + " messages\n" + 
            "Last message was sent at: " + lastMessage + "\n" +
            "Age: " + userData.age + "\n" + 
            "Times user left server: " + userData.times_left + "\n" +
            "Strikes: " + userData.strikes + "\n" + 
            userData.notes);

        } else {
        message.reply("You need to mention a user to get their data!");
        }
    }

    if(parts[0] === ';write') {
        if (message.mentions.members.first()) {

            var user = message.mentions.users.first();
            const id = user.id;
            const userData = guildData[id];

            message.channel.send("What data point do you want to edit? \n Accepted responses are: \n `message count` \n `age` \n `times left` \n `strikes` \n `notes` ")
            let filter = m => m.content.includes('message count' || 'age' || 'times left' || 'strikes' || 'notes') && !m.author.bot ;
            let collector = new Discord.MessageCollector(message.channel, filter);

    collector.on('collect', (m,col) => {
        var res = "";
        let mess = m.content;
        console.log(mess);
        if (mess == ("message count")) {res = 'message_count' } 
        if (mess == ("age")) {res = 'age' }
        if (mess == ("times left")) {res = 'times_left' }
        if (mess == ("strikes")) {res = 'strikes' }
        if (mess == ("notes")) {res = 'notes' }
        console.log(res);
        message.channel.send("Editing " + res + "\n The current value is:");

        if (res == "message count") {message.channel.send(userData.message_count) } 
        if (res == "age") {message.channel.send(userData.age)}
        if (res == "times left") {message.channel.send(userData.times_left)}
        if (res == "strikes") {message.channel.send(userData.strikes)}
        if (res == "notes") {message.channel.send(userData.notes)}

        collector.stop();
        message.channel.send("The value currently located inside of " + res + " will be overwritten, so if you want to edit the content directly, copy the message above, and paste it into the send box.");
        let filter = m => !m.author.bot;
        let collector2 = new Discord.MessageCollector(message.channel, filter);
    });

    collector2.on('collect', m => {
        var mess = m.content;
        if (res === "message count") {userData.message_count = mess } 
        if (res == "age") {userData.age = mess}
        if (res == "times left") {userData.times_left = mess}
        if (res == "strikes") {userData.strikes = mess}
        if (res == "notes") {userData.notes = mess}
        message.channel.send("Data saved!");
        console.log("Saving " + mess);
    
        var guildData = data[message.guild.id];
        var userData = guildData[message.author.id];
        var pointData = userData[res];
        console.log("old data");
        console.log(pointData);
        pointData = mess;
        console.log("new data");
        console.log(pointData);
        jsonfile.writeFileSync('data.json', data) //HERE IS THE ISSUE
        collector2.stop();
    });

    } else {
        message.reply("You need to mention a user to edit their data!");
    }
}
});

Client.login('TOKEN');

The data is stored as such:

'779904676879007744': {
    username: 'SeverMgr',
    message_count: 129,
    last_message: 1606028233445,
    age: 0,
    times_left: 0,
    strikes: 0,
    notes: ''

Thanks for any help!

12
  • Can you specify the issues you have with the JSON? Any error messages? Commented Nov 22, 2020 at 6:47
  • What does the error say? Commented Nov 22, 2020 at 6:47
  • I haven't gotten any errors, it's just not updating the file. Commented Nov 22, 2020 at 6:49
  • Does the code block run? Do you see the console.log above? Commented Nov 22, 2020 at 6:50
  • Yes, all the console.logs show up in the log. Commented Nov 22, 2020 at 6:50

1 Answer 1

1

The equal sign will create a copy of your variable. If you update the new variable it won't update the value of the previous one, since it's not a reference.
Example

var x = 1
var y = x
var y = 2
---
x will still be 1
y will be 2

So in your case you could do this to update the data variable

data[message.guild.id][message.author.id][res] = mess

Instead of this, since you are updating copies and not data directly.

var guildData = data[message.guild.id]
var userData = guildData[message.author.id]
var pointData = userData[res]
pointData = mess
Sign up to request clarification or add additional context in comments.

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.