Skip to content Skip to sidebar Skip to footer

How Can I Add Error Message In Discord.js?

I Create a Truth and dare Bot. My Prefix is + Now I want to add an error message to it. I have two variables 't' 'd' If anyone types +something which does not match my variable to

Solution 1:

Don't use separate message event handlers, use one. You can take advantage of that by using if else chain. You are trying to match the command through the chain, if no match was found, in else (meaning every previous check in the chain failed) you reply to the user by saying:

"Invalid command, type +help for help.".

Also check for the prefix at the beginning. If there is no prefix, return from the function. That way you don't have to write it to the if statements when matching the message content.

// Array of possible truth replies
const t = [
    "If you could be invisible, what is the first thing you would do?", 
    "What's the strangest dream you've ever had?",
    "What are the top three things you look for in a boyfriend/girlfriend?",
    "What is your worst habit?",
    "How many stuffed animals do you own?",
    "What is your biggest insecurity?"
];

// Array of possible dare replies
const d = [
    "Do a free-style rap for the next minute.",
    "Let another person post a status on your behalf.",
    "Hand over your phone to another player who can send a single text saying anything they want to anyone they want.",
    "Let the other players go through your phone for one minute.",
    "Smell another player's armpit",
    "Smell another player's barefoot.",
    "Tell everyone your honest opinion of the person who sent this command."
];

// Handle all commands here
client.on('message', message => {

    // Don't reply to itself
    if (message.author.id === client.user.id) return;

    // If there is no + (prefix) at the beginning of the message, exit function
    if (!message.content.startsWith(prefix)) return;

    // Remove the prefix from the message -> our command
    const command = message.content.substring(prefix.length);

    // Match the command
    if (command === "t") { // Truth
        const truth = t[Math.floor(Math.random() * t.length)];
        message.channel.send(truth);
    } else if (command === "d") { // Dare
        const dare = d[Math.floor(Math.random() * d.length)];
        message.channel.send(dare);
    } else if (command === "help") { // Help

        const help = new Discord.MessageEmbed()
            .setColor('#72dfa3')
            .setTitle(`Truth Or Dare`)
            .addFields(
                { name: '``+help``', value: 'For help' },
                { name: '``+t``', value: 'For Truth' },
                { name: '``+d``', value: 'For Your Dare' },
                { name: '``Created By``', value: 'AlpHa Coder [Labib Khan]' },
            )
            .setTimestamp()
            .setFooter(`${message.author.username}`, message.author.displayAvatarURL());

        message.channel.send(help);

    } else { // No match found, invalid command
        message.channel.send("Invalid command, type `+help` for help.");
    }

});

Post a Comment for "How Can I Add Error Message In Discord.js?"