Currently, I am in the process of developing a Discord bot using discord.js. As part of this project, I am incorporating JSON functionality to store information for individual users in a separate file. However, I have encountered an issue where an error message stating that planet is not defined
pops up at the line containing
if (bot.log[mentionedGuyName].planet == undefined) {
. Due to the extensive length of my code, I cannot include it all here.
The main objective of this particular code segment is to check if a user already has a "planet" assigned. If they do, the bot retrieves the value from the JSON file and sends it to the channel. If not, it selects a random one (additional code omitted).
I believe I have a basic understanding of why this error is occurring (undefined planet property), but I am uncertain about how to resolve it. Any assistance on how to define a JSON property or address this issue would be greatly appreciated by myself and my server members. Thank you in advance!
Below is a snippet of my JavaScript file:
let mentionedGuy = message.mentions.members.first();
let mentionedGuyName = null;
let noMentions = message.mentions.members.first() == false ||
message.mentions.members.first() == undefined;
if (noMentions) return;
else mentionedGuyName = mentionedGuy.user.username;
if (message.content.startsWith(prefix + "planet")) {
if (message.content.length > 7) {
if (bot.log[mentionedGuyName].planet == undefined) {
bot.log[mentionedGuyName] = {
planet: jMoon
}
fs.writeFile('./log.json', JSON.stringify(bot.log, null, 4), err => {
if (err) throw err;
});
message.channel.send(targeting);
message.channel.send(coords);
} else {
message.channel.send(bot.log[mentionedGuyName].planet);
}
}
}