Alright, I'm a fairly new person to javascript and have been using Discord.js to make one or two Discord Bots. I was recently working on a Medal Bot which would award medals to a user if a certain person gives it the mand. It'll look roughly like: /awardmedal The Copper Cross (INSERT USER@ HERE)
Whenever I ran the code and executed any of the medal mands, it would e up with the following:
Medals.js:21 var usertag = message.mentions.users.first().id;
^
TypeError: Cannot read property 'id' of undefined
I wanted to know if anyone can help and tell me what I should do to fix it, thanks. Here's the code that does this:
var prefix = "/"
client.on('ready', () => {
console.log("ZiloBot Loaded");
});
client.on("message", (message) => {
var usertag = message.mentions.users.first().id;
if (message.content.startsWith(prefix + "awardmedal " + "The Copper Cross " + usertag)) {
if (sjw.includes(message.author.id)) {
console.log("Awarding Copper Cross to " + usertag);
message.channel.send("Awarding Copper Cross to " + usertag);
};
};
client.login(mytokenissecret);
});
Don't worry about the sjw
variable, it is defined in a snippet of code before this one. My main issue is the fact that the id
is not defined.
Alright, I'm a fairly new person to javascript and have been using Discord.js to make one or two Discord Bots. I was recently working on a Medal Bot which would award medals to a user if a certain person gives it the mand. It'll look roughly like: /awardmedal The Copper Cross (INSERT USER@ HERE)
Whenever I ran the code and executed any of the medal mands, it would e up with the following:
Medals.js:21 var usertag = message.mentions.users.first().id;
^
TypeError: Cannot read property 'id' of undefined
I wanted to know if anyone can help and tell me what I should do to fix it, thanks. Here's the code that does this:
var prefix = "/"
client.on('ready', () => {
console.log("ZiloBot Loaded");
});
client.on("message", (message) => {
var usertag = message.mentions.users.first().id;
if (message.content.startsWith(prefix + "awardmedal " + "The Copper Cross " + usertag)) {
if (sjw.includes(message.author.id)) {
console.log("Awarding Copper Cross to " + usertag);
message.channel.send("Awarding Copper Cross to " + usertag);
};
};
client.login(mytokenissecret);
});
Don't worry about the sjw
variable, it is defined in a snippet of code before this one. My main issue is the fact that the id
is not defined.
message.mentions.users
? Also, the /
prefix is already used by Discord, you should not use it, as it may cause errors.
– Seblor
Commented
Mar 19, 2019 at 11:10
Improved your code a bit:
client.on('ready', () => {
console.log("ZiloBot Loaded");
});
client.on("message", (message) => {
const prefix = "/"; // using ES6
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const cmdName = args.shift().toLowerCase();
if (cmdName === 'awardmedal') {
// checking if user inluded correct medal name in message.
let mention = message.mentions.users.first();
// checking if message don't have a user mention
if (!mention) return message.channel.send('You need to mention a user.');
let medals = ['The Copper Cross']; // creating array of medals in case u want to add more medals later on
let medal = args.join(' ').replace(`<@!${mention.id}>`, '').trim(); // removing mention and spaces from message string
if (!medals.map(m => m.toLowerCase()).includes(medal.toLowerCase())) {
message.channel.send(`Please choose one from a list:\n${medals.join(', ')}`);
return;
}
if (!sjw.includes(message.author.id)) {
// if user in not in "sjw"
return;
}
console.log(`Awarding ${medal} to ${mention.name}`);
message.channel.send(`Awarding ${medal} to ${mention}`);
}
};
client.login(mytokenissecret);
To solve your main issue first you need to check if user mention exist and only after get id.
let mention = message.mentions.users.first();
if (mention) console.log(mention.id);
message.mentions.users.first()
is not a thing
Servers do not have users, they have MEMBERS, so use
message.mentions.members.first().id