0

I have been asked to create a Discord bot that displays a list of guild member usernames and the date they last posted in Discord, it should also display a seperate list of members who have not posted anything at all. In order to achieve this the information is pulled from the Discord API and saved in a JSON file. If a new member joins then the message date is set to 00/00/0000 so I can then use an if statement to pull these members into a seperate list.

bot.on("ready", async () => {
    console.log(`${bot.user.username} is online!`);

    const guild = bot.guilds.cache.get('SERVER ID NUMBER');
        guild.members.fetch().then(member => {
            member.forEach(member => {
                let memberArray = JSON.parse(fs.readFileSync("./messages.json", "utf8"));
                    if(!member.user.bot) {
                        if(!memberArray[member.user.id]) {
                        memberArray[member.user.id] = {name: member.user.username, date: "00/00/0000"}
                        }
                    } 
                        
    fs.writeFileSync("./messages.json", JSON.stringify(memberArray), (err) => {
        if (err) console.log(err)
    })   
            })
        })
});

bot.on("guildMemberAdd", (member) => {

    let memberArray = JSON.parse(fs.readFileSync("./messages.json", "utf8"));

    if(!member.user.bot) {
        if(!memberArray[member.user.id]) {
            memberArray[member.user.id] = {name: member.user.username, date: "00/00/0000"}
            }
        }
                        
    fs.writeFileSync("./messages.json", JSON.stringify(memberArray), (err) => {
        if (err) console.log(err)
    })
});

bot.on("guildMemberRemove", (member) => {
    let memberArray = JSON.parse(fs.readFileSync("./messages.json", "utf8"));

    if(member.user.bot) {
        return;
    } else {
        delete memberArray[member.user.id]
    }
                        
    fs.writeFileSync("./messages.json", JSON.stringify(memberArray), (err) => {
        if (err) console.log(err)
    })
});

bot.on("message", async message => {
    if (message.author.bot) return; 

    const guild = bot.guilds.cache.get('SERVER ID NUMBER')

        guild.members.fetch().then(member => {

            let memberArray = JSON.parse(fs.readFileSync("./messages.json", "utf8"));

                member.forEach(member => {
                    if(member.user.bot) {
                        return;
                    }
                    
                var d = new Date(message.author.lastMessage.createdTimestamp);
                    day = d.getDate();
                    month = d.getMonth() + 1;
                    date = ("0" + d.getDate()).slice(-2) + "/" 
                    + ("0" + (d.getMonth() + 1)).slice(-2) + "/" 
                    + d.getFullYear();
                    if (message.author.username == memberArray[member.user.id].name) {
            memberArray[member.user.id].date = date;
                    }
    })
    fs.writeFile("./messages.json", JSON.stringify(memberArray),(err) => {
    if (err) console.log(err)   
     })
})

    let prefix = botconfig.prefix;
    let messageArray = message.content.split(" ");
    let cmd = messageArray[0].toLowerCase();
    let args = messageArray.slice(1);

    let commandfile = bot.commands.get(cmd.slice(prefix.length));
    if (commandfile) commandfile.run(bot, message, args, tools);
});

bot.login(botconfig.token)

Everything works fine in my test server but when I add the bot to the live server the date for any new members is set as the date they join instead of 00/00/0000.

I have ran various tests, added the same bots to my test server incase they were causing some kind of issue and I can't figure out why it works on one server but not the other.

If anybody has any ideas on what it could be or any more tests I can try I would be really grateful.

SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107
  • This code works fine. However, as we don't have the rest of the code, we can't rule out an indirect effect by another function. Did you try to isolate function one by one to check that nothing else is affecting your file or the variable? Did you check the content of the file at each step to see if the date change and when? – JackRed May 13 '21 at 10:27
  • @JackRed After more testing I have realised if I use my test server ID number it works fine, however, as soon as I change it to the other servers ID number it starts adding the wrong date – Nichola Robson May 13 '21 at 18:58
  • ` const guild = bot.guilds.cache.get('SERVER ID NUMBER');` – Nichola Robson May 13 '21 at 19:04

1 Answers1

0

When someone join a server, there is a system message (if enabled) that will pop:

enter image description here

The problem is that, on discord.js side, the system message's author is the user who joined. So when the user join the server and you assign the time to '00/00/0000' it is instantly overwritten by the date of the system message.

You can actually ignore those system message by checking the type of the message. You have the list of the different type here: discord.js.org. A normal message will have a type equal to DEFAULT while a system message when a user join will be GUILD_MEMBER_JOIN.


Small improvement remark:

JackRed
  • 1,020
  • 1
  • 12
  • 25