skip to Main Content

i want to make command that can give me information about someone that i mention like !info @Someone i try code below, but didnt work.

This is the schema

const mongoose = require('mongoose');

const Schema = mongoose.Schema;
const profileSchema = new Schema({
    _id: mongoose.Schema.Types.ObjectId,
    userID: String,
    nickname: String,
    ar: Number,
    server: String,
    uid: Number,
});

module.exports = mongoose.model("User", profileSchema);

and this is what i try, but show nothing, didnt show any error sign.

client.on("message", async msg => {
  let member = msg.mentions.users.first().username
  if (msg.content === `!info @${member}`){ 
    userData = await User.findOne({userID : msg.mentions.users.first().id});
     if (userData) {
        const exampleEmbed = new MessageEmbed()
        .setColor('#808080')
        .setTitle('Data Member')
        .setDescription(`**Nickname :** ${userData.nickname}n**Adventure Rank :** ${userData.ar}nServer: ${userData.server}n**User ID :** ${userData.uid}`)
        .setThumbnail(msg.author.avatarURL())
        msg.reply({ embeds: [exampleEmbed] });
     } else{
      msg.reply("Please registration first")
     }
  }
}
);

3

Answers


  1. Going through your code, I found these errors.

    first of all you need members not users in message.mentions.members.first().

    Second of all, you need to define UserData first like const UserData = ...

    client.on("message", async msg => {
      let member = msg.mentions.members.first()
      if (msg.content === `!info @${member}`){ 
        User.findOne({userID : member.id}, async (err, userData) => {
         if (userData) {
            const exampleEmbed = new MessageEmbed()
            .setColor('#808080')
            .setTitle('Data Member')
            .setDescription(`**Nickname :** ${userData.nickname}n**Adventure Rank :** ${userData.ar}nServer: ${userData.server}n**User ID :** ${userData.uid}`)
            .setThumbnail(msg.author.avatarURL())
            msg.reply({ embeds: [exampleEmbed] });
         } else{
          msg.reply("Please registration first")
         }
      }
     });
    });
    

    Let me know if it works after fixing these errors.
    Also message event is depricated so try using MessageCreate instead from now on

    Login or Signup to reply.
  2. Change the if condition. How Discord Mentions Work

    Discord uses a special syntax to embed mentions in a message. For user mentions, it is the user’s ID with <@ at the start and > at the end, like this: <@86890631690977280>.

    if (msg.content === `!info ${message.mentions.users.first()}`)
    

    For example:

    const member = msg.mentions.users.first();
    if (msg.content === `!info ${member}`){ 
        User.findOne({ userID: member.id }, (err, user) => {
            if (err) return console.error(err);
            if (!user) return msg.reply("User not found");
            console.log(user);
        });
    }
    
    Login or Signup to reply.
  3. By seeing your code, it might shuffle all of your .first() lets modify your code.

    client.on("message", async msg => {
    let member = msg.mentions.members.first() || msg.guild.members.fetch(args[0]); //You can also use their ID by using these
    if (msg.content === `!info ${member.username || member.user.username}`) { //then adding the user.username
    const userData = await User.findOne({
      userID: member.id || member.user.id //same as here
    }); //userData shows as "any" so you need to change it to const userData
    if (userData) {
      const exampleEmbed = new MessageEmbed()
        .setColor('#808080')
        .setTitle('Data Member')
        .setDescription(`**Nickname :** ${userData.nickname}n**Adventure Rank :** ${userData.ar}nServer: ${userData.server}n**User ID :** ${userData.uid}`)
        .setThumbnail(msg.author.avatarURL())
      msg.reply({
        embeds: [exampleEmbed]
      });
    } else {
      msg.reply("Please registration first")
    }
    }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search