Search code examples
javascriptdiscorddiscord.js

discord.js if args null its not sending the error message


I have a command that uses 3 args, a mentioned user one, a mentioned channel one and a text arg. But every time i miss a arg, instead of showing a error message, its shows a error in the console. The code is like this:

const Discord = require('discord.js')
const { MessageEmbed } = require('discord.js');

module.exports = {
  name: 'rejeitar',
  category: 'Premium',
  description: 'rejeitar alguem na org '
  ,
run: async (client, message, args, user, guild) => {

  if(message.member.roles.cache.some(r => r.name === "[🔥] Gestor tickets")){
  
let member = message.mentions.members.first();
let channel = message.mentions.channels.first();
    const motivo = args.slice(2).join(' ');

    const rejeitado2 = new MessageEmbed()
    .setColor('#ff0000')
    .setTitle('**📝❱Resultado cands**')
    .setDescription('**Rejeitado**')
    .addFields(
        { name: '**💼❱ Rejeitado pelo Staff**', value: `${message.author.tag}` },
    { name: '**🕒❱ Data**', value: `${message.createdAt}` },
    { name: '**🕒❱ Motivo**', value: `${motivo}` },
        { name: '**👨‍🦲❱ Membro Rejeitado**', value:`${member}`, inline: true },
    )
    .setTimestamp()
    .setFooter({ text: 'Bot feito por Chain#6988' });

     ////---------------LOG EMBED-------------/////
      
     const rejeitado = new MessageEmbed()
    .setColor('#ff0000')
    .setTitle('**📝❱Infelizmente não foste aceite nos TDT**')
    .setDescription('**Tenta denovo mais tarte **')
    .addFields(
        { name: '**💼❱Rejeitado pelo Staff **', value: `${message.author.tag}` },
        { name: '**🕒❱Data**', value: `${message.createdAt}` },
    { name: '**🕒❱ Motivo**', value: `${motivo}` },
        { name: '**👨‍🦲❱Membro Rejeitado**', value:`${member}`, inline: true },
    )
    .setFooter({ text: 'Bot feito por Chain#6988' });
if(channel && member ){ 
channel.send({ embeds: [rejeitado] });
channel.send (`${member}`)
   client.channels.cache.get('1002892850385657866').send({ embeds: [rejeitado2] });
    client.channels.cache.get('1002892850385657866').send(`${member}`);
       message.react("✅")
    message.channel.send("Clica no ✅ para apagar o ticket!")
 }
else
message.channel.send("**ERRO**\nVerifica se podes usar o comando ou se esta correto!(.aceitar #ticket  @pessoa rejeitada motivo")


  function filter(reaction, user) {
          return reaction.emoji.name === '✅' && user.id === message.author.id;
      }
// Increase or decrease the time based on your needs.
const collector = message.createReactionCollector({ filter, time: 1500000, max: 1 });

    collector.on("collect", (reaction, user) => {

/* Because of the way we defined our filter, there is no need to check
if the user reacted with any other emoji than the ✅ */

channel.delete();
})

// and if the user doesn't react with anything in the time limit
collector.on('end', collected => {
 if(collected.size < 1) {
  return message.channel.send("o tempo acabou, vais ter de fechar o ticket manualmente")
   
   
 }
})
}
}
}

The error that shows in the console:

/home/runner/turqia-bot/node_modules/discord.js/src/util/Util.js:428
    if (!allowEmpty && data.length === 0) throw new error(errorMessage);
                                                ^

RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values must be non-empty strings.
   

And its suppost to send a error message to the channel if it has no args or is missing one. If you guys know a fix to this, help me out. Thanks!


Solution

  • Your error message is not sent because you're using an if...else statement. Your initial condition returns true, so your else statement will not execute.

    What you can do to solve your problem, is simply checking if there are 3 arguments used:

    if(message.member.roles.cache.some(r => r.name === "[🔥] Gestor tickets")){
    
      // or the equivalent index of your last argument
      if(!args[2]) {
      return message.channel.send("**ERRO**\nVerifica se podes usar o comando ou se esta correto!(.aceitar #ticket  @pessoa rejeitada motivo");
      }
    
    // rest of your code
    }
    

    You can also enveil your code in a try...catch statement, and send the message on any error. This is not the best way to do it, but it still works.