-2

Just recently finished up codeacademy's Learn JavaScript and currently working my way through the Discord.js guide. Learning how to command handle and came across something I didn't quite understand.

const fs = require('fs');

module.exports = {
    name: 'reload',
    description: 'Reloads a command',
    execute(message, args) {
        const commandName = args[0].toLowerCase();
        const command = message.client.commands.get(commandName)
            || message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

        if (!command) return message.channel.send(`There is no command with name or alias \`${commandName}\`, ${message.author}!`);
    },
};

These lines of code:

message.client.commands.get(commandName)
            || message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

1. I am confused as to what "||" means (is it 'or', if so I do not understand this syntax)

2. I do not know where 'cmd' is coming from.

Edit: Embarrassing mistake on my part. I was just a bit confused. All cleared up thank you.

  • 1
    1. https://stackoverflow.com/q/2802055/3001761 2. What do you mean *”where [it’s] coming from”*? It’s the *parameter* to the function, read e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find. – jonrsharpe Mar 17 '21 at 22:33
  • Re ^^: If `cmd => ...` doesn't look like a function to you, fair enough! See http://stackoverflow.com/questions/24900875/whats-the-meaning-of-an-arrow-formed-from-equals-greater-than-in-javas – T.J. Crowder Mar 17 '21 at 22:37

1 Answers1

1
  1. Yes, it is the 'or' operator. In this case if message.client.commands.get(commandName) evaluates to true, it will assign that value to 'command'. Otherwise, it will assign the other value to the right of ||

  2. 'find' iterates values in an array until it evaluates to true. 'cmd' is the currently iterated value.

olawrdhalpme
  • 180
  • 8