L Z
L Z

Reputation: 59

Discord js random mention bot

I'm making a command that randomly give you percentage when you do -komanda. I just couldn't make to it says if no one is mentioned to mention someone and doesn't give me a undefined is 90%. This is my code:

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require('discord.js');


module.exports = class SayCommand extends BaseCommand {
  constructor() {
    super('komanda', 'fun', []);
  }

async  run(client, message, args) {
  var rating = Math.floor(Math.random() * 100) + 1;
    let mention = message.mentions.users.first()
    const mentionedMember = message.mentions.members.first();
    const messageToSay = args.join(" ");
    const sayEmbed = new Discord.MessageEmbed()
    message.channel.send(`${mention} je \xa0${rating}%\xa0\ bad man`)

  
      try {
      } catch (err) {
          console.log(err);
          message.channel.send("JA ZIVKOVIC SLOBODAN NE SMEM TO DA KAZEM");
        }
      }

  }

When i do -komanda @USER it will show: @USER je 90% bad man but if i dont mention anyone it will say undefined

Upvotes: 1

Views: 202

Answers (2)

NouNou
NouNou

Reputation: 1

Before <channel>.send(...) check if there is mention:

if (!mention) return message.channel.send("Please mention");

Upvotes: 0

NullDev
NullDev

Reputation: 7303

If you want to output a warning when no user has been mentioned, just check if mention is truthy:

if (!mention) return // ...

For example, in your code:

let rating = Math.floor(Math.random() * 100) + 1;
let mention = message.mentions.users.first();

if (!mention) return message.channel.send("Please mention a user!");

message.channel.send(`${mention} je \xa0${rating}%\xa0\ bad man`);

Upvotes: 2

Related Questions