javascript - Having trouble with message.mentions.users.first().id definition - Stack Overflow

admin2025-04-20  0

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.

Share Improve this question edited Mar 19, 2019 at 16:23 Federico Grandi 6,7865 gold badges33 silver badges51 bronze badges asked Mar 19, 2019 at 10:07 OvalandirOvalandir 211 gold badge1 silver badge3 bronze badges 1
  • 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. – Seblor Commented Mar 19, 2019 at 11:10
Add a ment  | 

2 Answers 2

Reset to default 2

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
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123776a286304.html

最新回复(0)