Cannot find value in JSON file of mentioned user (TypeError: Cannot read properties of undefined).

I'm trying to check the value of variables ally1, ally2, ally3, etc, and when using message.author.id, it works fine. However, as you will see below, I cannot get it to work for mentioned users:
if (args[0] == "ally") {
let UserJSON = JSON.parse(Fs.readFileSync("./DB/users.json"));

let mention1 = message.mentions.users.first;
let mention2 = message.mentions.users.first(2)[1];
let mention3 = message.mentions.users.first(3)[2];
let mention4 = message.mentions.users.first(4)[3];


if (!UserJSON[message.author.id]) {
message.channel.send("You must register a nation to do this!");
} else {
if (args[1] == "create") {

if (UserJSON[message.author.id].isLeader == 1) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally1 > 0) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally2 > 0) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally3 > 0) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally4 > 0) { message.channel.send("You are already in an alliance!"); return }

if (UserJSON[mention1.id].ally1 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (UserJSON[mention1.id].ally2 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (UserJSON[mention1.id].ally3 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (UserJSON[mention1.id].ally4 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (args[0] == "ally") {
let UserJSON = JSON.parse(Fs.readFileSync("./DB/users.json"));

let mention1 = message.mentions.users.first;
let mention2 = message.mentions.users.first(2)[1];
let mention3 = message.mentions.users.first(3)[2];
let mention4 = message.mentions.users.first(4)[3];


if (!UserJSON[message.author.id]) {
message.channel.send("You must register a nation to do this!");
} else {
if (args[1] == "create") {

if (UserJSON[message.author.id].isLeader == 1) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally1 > 0) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally2 > 0) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally3 > 0) { message.channel.send("You are already in an alliance!"); return }
if (UserJSON[message.author.id].ally4 > 0) { message.channel.send("You are already in an alliance!"); return }

if (UserJSON[mention1.id].ally1 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (UserJSON[mention1.id].ally2 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (UserJSON[mention1.id].ally3 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
if (UserJSON[mention1.id].ally4 > 0) { message.channel.send(mention1.nationname + "is already allied!"); return }
I'm being thrown error: TypeError: Cannot read properties of undefined (reading 'ally1'). Below is a exceprt of the JSON file:
"370619948776816640": {
"nationname": "j",
"yearlydate": 1920,
"armyprodcap": 0,
"navyprodcap": 0,
"airforceprodcap": 0,
"maxprov": 15,
"nukes": 0,
"tradelimit": 0,
"color": "95DFE6",
"gold": 100,
"steel": 50,
"alum": 25,
"food": 25,
"oil": 25,
"nationalpoint": 3,
"rare": 0,
"infantry": 0,
"medic": 0,
"support": 0,
"scout": 0,
"lt": 0,
"mt": 0,
"ht": 0,
"art": 0,
"aa": 0,
"motor": 0,
"mecha": 0,
"fighter": 0,
"heavyfighter": 0,
"stratbomber": 0,
"heavybomber": 0,
"navalbomber": 0,
"destroyer": 0,
"lightcruiser": 0,
"heavycruiser": 0,
"battleship": 0,
"vessel": 0,
"carrier": 0,
"submarine": 0,
"nuclearsubmarine": 0,
"goldminedouble": 0,
"alumminedouble": 0,
"goldmine": 0,
"alummine": 0,
"factorydouble": 0,
"factory": 0,
"farmdouble": 0,
"farm": 0,
"oilrigdouble": 0,
"oilrig": 0,
"reactor": 0,
"fort": 0,
"port": 0,
"airfield": 0,
"yearlytech": 0,
"industrialrevolution": 0,
"increasedproduce": 0,
"fasterresearch": 0,
"inexpensiveresearch": 0,
"productionreduction": 0,
"militarydevelopments": 0,
"developtanks": 0,
"developaircraft": 0,
"navalbombing": 0,
"strategicbombing": 0,
"heavybombing": 0,
"underthesea": 0,
"bombsaway": 0,
"landsupport": 0,
"scoutdivisions": 0,
"supportdivisions": 0,
"nuclearreaction": 0,
"militaryissues": 4,
"industryissues": 4,
"researchissues": 4,
"landissues": 4,
"conscriptionissues": 4,
"isAllied": 0,
"isLeader": 0,
"allyInvite": 0,
"allianceName": "null",
"ally1": 0,
"ally2": 0,
"ally3": 0,
"ally4": 0
}
"370619948776816640": {
"nationname": "j",
"yearlydate": 1920,
"armyprodcap": 0,
"navyprodcap": 0,
"airforceprodcap": 0,
"maxprov": 15,
"nukes": 0,
"tradelimit": 0,
"color": "95DFE6",
"gold": 100,
"steel": 50,
"alum": 25,
"food": 25,
"oil": 25,
"nationalpoint": 3,
"rare": 0,
"infantry": 0,
"medic": 0,
"support": 0,
"scout": 0,
"lt": 0,
"mt": 0,
"ht": 0,
"art": 0,
"aa": 0,
"motor": 0,
"mecha": 0,
"fighter": 0,
"heavyfighter": 0,
"stratbomber": 0,
"heavybomber": 0,
"navalbomber": 0,
"destroyer": 0,
"lightcruiser": 0,
"heavycruiser": 0,
"battleship": 0,
"vessel": 0,
"carrier": 0,
"submarine": 0,
"nuclearsubmarine": 0,
"goldminedouble": 0,
"alumminedouble": 0,
"goldmine": 0,
"alummine": 0,
"factorydouble": 0,
"factory": 0,
"farmdouble": 0,
"farm": 0,
"oilrigdouble": 0,
"oilrig": 0,
"reactor": 0,
"fort": 0,
"port": 0,
"airfield": 0,
"yearlytech": 0,
"industrialrevolution": 0,
"increasedproduce": 0,
"fasterresearch": 0,
"inexpensiveresearch": 0,
"productionreduction": 0,
"militarydevelopments": 0,
"developtanks": 0,
"developaircraft": 0,
"navalbombing": 0,
"strategicbombing": 0,
"heavybombing": 0,
"underthesea": 0,
"bombsaway": 0,
"landsupport": 0,
"scoutdivisions": 0,
"supportdivisions": 0,
"nuclearreaction": 0,
"militaryissues": 4,
"industryissues": 4,
"researchissues": 4,
"landissues": 4,
"conscriptionissues": 4,
"isAllied": 0,
"isLeader": 0,
"allyInvite": 0,
"allianceName": "null",
"ally1": 0,
"ally2": 0,
"ally3": 0,
"ally4": 0
}
Obviously I'm not a genius and searching for answers I found a lot of very rude responses :/ Thanks for the help
2 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
tilted
tiltedOP9mo ago
[email protected] Node v18.13.0 LMFAO
Want results from more Discord servers?
Add your server