'discord.js guildMemberAdd() not adding correct value to Object
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.
Solution 1:[1]
When someone join a server, there is a system message (if enabled) that will pop:
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:
- when you get a message from an user, you can request the guild directly from
message.guildinstead of fetching it with the id every time. If your bot accept dm, you can use themessage.channel.type(the different values are listed on discord.js.org) - You can format a date using toLocaleDateString. See https://stackoverflow.com/a/34015511/8040287 for details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JackRed |

