Having trouble with message.mentions.users.first().id definition

17,239

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);
Share:
17,239

Related videos on Youtube

Ovalandir
Author by

Ovalandir

new guy to javascript and overall coding, might start python sure

Updated on June 04, 2022

Comments

  • Ovalandir
    Ovalandir almost 2 years

    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 command. It'll look roughly like: /awardmedal The Copper Cross (INSERT USER@ HERE) Whenever I ran the code and executed any of the medal commands, it would come 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.

    • Seblor
      Seblor about 5 years
      Are you sure the message contains a mention ? What is showed if you try to log message.mentions.users ? Also, the / prefix is already used by Discord, you should not use it, as it may cause errors.
  • kcode
    kcode over 3 years
    Read the docs, message.mentions.users exists. discord.js.org/#/docs/main/stable/class/…