Search code examples
discord.jscommandbots

How do I stop the command from happening if the requirements for it to work aren't sert Discord.js


My command, bt!sendlove @User currently works fine. But there is something that I'd like to work out.

In order for the command to work, you have to tag a user which will then be converted to a variable and used in text: Sent love to User

The problem occurs when typing the command with no user, hence: bt!sendlove. This makes the bot crash. I've tried experimenting with different ways to resolve this:

client.on("messageCreate", (message) => {
     if (message.content.startsWith("bt!sendlove")) {
          if (userToMention === "") {
               message.react('❌')
               .then(message.channel.send("Invalid command use."))
               .then(message.channel.send("Please use the correct format: `bt!sendlove @User`"));
          }

          else if (userToUser === "") {
               message.react('❌')
               .then(message.channel.send("Invalid command use."))
               .then(message.channel.send("Please use the correct format: `bt!sendlove @User`"));
          }
          const userToMention = message.mentions.users.first();
          const userToUser = message.mentions.users.first().username;
          const messageAuthor = message.author.username;

(the rest of the code is after this part)

None of the solutions worked. Is there any way I can make the bot react to the message, send the messages and stop the command from continuing?

Thanks.


Solution

  • Is it possible that when there is no tagged user it returns undefined? userToMention === "" only returns true (and therefore executes the if statement) if the value of the variable is an empty string (""). Using !userToMention would be true if the value of the variable is null, undefined, NaN or an empty string (logical NOT operator).

    But the reason why the bot is probably crashing is that in the case of there not being a tagged user, it's trying to access a variable that doesn't exist. In that case you can use optional chaining which checks that the object exists before accessing something inside it. You use it by adding a question mark. It would look something like this:

    const userToMention = message.mentions.users?.first();
    const userToUser = message.mentions.users?.first().username;
    

    But if the problem is with the first() method returning undefined, then it's a bit more difficult. But I also just noticed that the username is included in userToMention since they're trying to access the same object. The above can be rewritten as:

    const userToMention = message.mentions.users.first();
    const userToUser = userToMention?.username;
    

    As to stopping the function from running, you can use return inside the if statement to return the function. I don't know what the best convention is, but here's an example:

    return (
        message.react('❌')
        .then(message.channel.send("Invalid command use."))
        .then(message.channel.send("Please use the correct format: `bt!sendlove @User`"));
    )
    

    Since userToUser is based on userToMention, we know what userToUser will be undefined is userToMention will be undefined. So we don't actually need to create two if statements to check for that, one will be enough. It is also good practise to create the variables before trying to access them in the code. The resulting code would look like this:

    client.on("messageCreate", (message) => {
         if (message.content.startsWith("bt!sendlove")) {
             const userToMention = message.mentions.users.first();
             const userToUser = userToMention?.username;
             const messageAuthor = message.author.username;
          
             if (!userToUser) {
                 return (
                     message.react('❌')
                         .then(message.channel.send("Invalid command use."))
                         .then(message.channel.send("Please use the correct format: `bt!sendlove @User`"));
                 )
             }