Reputation: 7
The thing is I am trying to make a command which show how much coins a user has, but every time I type the command it just returns „cannot read property „id” of undefined”.
Here is the code:
const Discord = require("discord.js");
let coins = require("../coins.json");
module.exports.run = async (bot, message, args) => {
//!coins
let pUser = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
if(!coins[pUser.id]){
coins[pUser.id] = {
coins: 0
};
}
if(!coins[message.author.id]){
coins[message.author.id] = {
coins: 0
};
}
let pCoins = coins[pUser.id].coins;
let uCoins = coins[message.author.id].coins;
if (!pUser) {
message.channel.send(`**${message.author}**: Ai manglit **${uCoins}** puncte de neață!`);
} else {
message.channel.send(`**${pUser}**: Ai manglit **${pCoins}** puncte de neață!`);
}
}
module.exports.help = {
name: "neti"
}
Also here is my coins.json file:
{
"108225935509057536":{
"coins":3,
"id":"108225935509057536"
},
"513103473571790857":{
"coins":579
},
"274234269944119296":{
"coins":436
},
"289874882974711808":{
"coins":48
},
"282255872871235584":{
"coins":372
},
"260746723805233153":{
"coins":721
},
"595994062864121872":{
"coins":18
},
"225239396339679232":{
"coins":362
},
"346946888634728450":{
"coins":59
},
"186871446125936644":{
"coins":159
},
"279263622247743488":{
"coins":669
},
"126321478550552576":{
"coins":1
},
"469899490686664714":{
"coins":1
},
"242381361158029314":{
"coins":13
},
"224095971955441665":{
"coins":11
},
"547093167040757770":{
"coins":26
},
"252882616297062400":{
"coins":4
},
"261602781981310976":{
"coins":59
},
"254002772679196682":{
"coins":6
},
"371414290156093440":{
"coins":20
},
"188347467874435073":{
"coins":9
},
"449120976870047744":{
"coins":122
},
"286178947148939265":{
"coins":10
},
"539794625972404225":{
"coins":0
},
"491701834193305600":{
"coins":2
},
"280730839946493953":{
"coins":13
}
}
Upvotes: 0
Views: 476
Reputation: 2722
This block will always undefined, pUser its a guild.member and dont have property id
if(!coins[pUser.id]){
coins[pUser.id] = {
coins: 0
};
}
The fist part of this code will return collection of message.author
so you will see [object,object]
if (!pUser) {
message.channel.send(`**${message.author}**: Ai manglit **${uCoins}** puncte de neață!`);
} else {
message.channel.send(`**${pUser}**: Ai manglit **${pCoins}** puncte de neață!`);
}
The right way is:
const Discord = require("discord.js");
let coins = require("../coins.json");
module.exports.run = async (bot, message, args) => {
let targetMember = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
if(!targetMember) targetMember = message.member
if(!coins[targetMember.user.id]){
coins[targetMember.user.id] = {
coins: 0
};
}
let pCoins = coins[targetMember.user.id].coins;
message.channel.send(`**${targetMember.user.id}**: Ai manglit **${pCoins}** puncte de neață!`);
}
module.exports.help = {
name: "neti"
}
Upvotes: 1
Reputation: 183
Actually in your code you get the user that is mentionned or the user you get if you type an id in first arg
/coin @john#0000 or /coin 192367487362 (this is an example)
You have to add a condition to protect your code from error like that :
module.exports.run = async (bot, message, args) => {
//!coins
let pUser = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
if(!pUser) {
// Do something if no user was mentionned and no id was sent with the first argument
return;
}
if(!coins[pUser.id]){
coins[pUser.id] = {
coins: 0
};
}
if(!coins[message.author.id]){
coins[message.author.id] = {
coins: 0
};
}
let pCoins = coins[pUser.id].coins;
let uCoins = coins[message.author.id].coins;
if (!pUser) {
message.channel.send(`**${message.author}**: Ai manglit **${uCoins}** puncte de neață!`);
} else {
message.channel.send(`**${pUser}**: Ai manglit **${pCoins}** puncte de neață!`);
}
}
Upvotes: 0
Reputation: 26
const data = {"108225935509057536": { coins: 3, id: "108225935509057536" }};
console.log(data["108225935509057536"]);
try this
Upvotes: 0