Reason after blacklisting command Discord.js

Tags: , , ,



I want to add a reason to my blacklists (with the command !blacklist {userid} {reason}) which are visible in the embeds below like .addField (“💬 Reason:”, somecode) how can I fix this?

  if (command === "blacklist") {
    if(!config["allowed-users"].includes(message.member.id)) return;
    const user = client.users.cache.get(args[0]);
    if(!user) {
      return message.channel.send("This user does not exist")
    }
    if(blacklist.has(user.id)) {
      return message.channel.send("This user is already on the blacklist")
    }

    blacklist.set(user.id, 'blacklisted');
    let set = db.fetch(`g_${message.guild.id}`);
    var embed = new Discord.MessageEmbed()
    .setTitle(":warning: Blacklisted :warning:")
    .setColor('#fc5a03')
    .addField("👮 Moderator:", message.author.tag)
    .addField("👤 User:", user.username)
    .addField("🆔 User ID:", user.id)
    .addField("🕒 Blacklisted on:", message.createdAt)
    .setFooter("© 2020 - 2021 GlobalChat", "https://cdn.discordapp.com/avatars/759021875962576916/cc32b2b08fdd52ae86294516d34532c5.png?size=128")
    .setThumbnail(user.avatarURL({ dynamic:true }))
    .addField("Unblacklist?", "Please contact <@267818548431290369> or <@331736522782932993>");

    client.guilds.cache.forEach(g => {
      try {
        client.channels.cache.get(db.fetch(`g_${g.id}`)).send(embed);
      } catch (e) {
        return;
      }
    });


  } 

Answer

First you’ll want to check if there is no reason, this can be simple done by checking, for both approaches, if the second argument is undefined, like so

if (args[1] === undefined) {
    const reason = "No reason.";
}

This solution will work for both approaches, since if the second argument is undefined there can be no more after it

You could take reason as an argument. Inside the command add

const reason = args[1];

OR if you wanted to have the rest of the blacklist args dedicated to the reason you could add something along the lines of

let reason = ""
for (let i = 1; i < args.length; i++) { 
// It's very important that i starts as 1, so we do not take the first argument into account for the reason
    reason += args[i];
}

And then you can add to the embed

.addField("💬 Reason:", reason); 

If you went with the first approach, the blacklist command would work like this

!blacklist 012345678910111213 the_reason_here
// or
!blacklist 012345678910111213 reason

The limitation to this approach is that a multi word reason isn’t very intuitive.

If you went with the second approach though, the blacklist command would work like this

!blacklist 012345678910111213 The reason the user was banned and it can go on and on and on as long as the writer wants


Source: stackoverflow