Home > Software engineering >  duscird.js get the name of the user involved
duscird.js get the name of the user involved

Time:04-02

I have a command in which one user virtually kisses another

The output is a message @User1 kissed @User2

But I want that instead of tags on the user, only names were written, like this so that it was user1 kissed user2

In theory, everything should work like this

`**${message.author.username}** kissed **${userToKiss.username}**`

But if the name for message.author is defined, then there is none for userToKiss and I end up with this

user1 kissed undefined

How can I get the name for the second user?

const { Command } = require('discord.js-commando');
const Discord = require('discord.js');

module.exports = class KissCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'kiss',
      memberName: 'kiss',
      group: 'reywl',
      description: 'Kiss the mentioned user',
      guildOnly: true,
      args: [
        {
          key: 'userToKiss',
          prompt: 'Please select the member you want to kiss.',
          type: 'member',
          default: 'isempty',
          wait: 0.0001
        }
      ]
    });
  }
  
  run(message, { userToKiss }) {
    if (userToKiss == 'isempty') {
        return message.channel.send('Please select the member you want to kiss.')}
    if (userToKiss.id == message.author.id) {
      return message.channel.send('You cant kiss yourself!');
    }
    else {
        const embed = new Discord.MessageEmbed()
        .setDescription(`**${message.author}** kissed **${userToKiss}**`)

        message.channel.send(embed)
    }
    setTimeout(() => message.delete(), 1000)
}};

CodePudding user response:

This is how it works

const kissed = await message.guild.members.fetch(userToKiss.id);

${kissed.user.username}

CodePudding user response:

For deep explanation for your work, to do this you need to code first about the member, we always do about member and author

Author is for the author itself and the member for your members who are you going to call

I don't know if commando and normal command handlers are the same but here's the code what you wanted.

const member = message.mentions.members.first() //this is the way how you ping a member
${member.user.username} //the word user is the user member

for the author you can only do this

${message.author.username}

You can also ping a member using their Id's

const member = message.guild.members.cache.get(args[0])
${member.user.username}

So the main output is

${message.author.username} kissed ${member.user.username}
  • Related