discordjs / guide

The official guide for discord.js, created and maintained by core members of its community.
https://discordjs.guide
MIT License
1.57k stars 2.31k forks source link

Is is possible to have a command with no args with the dynamic commands approach #604

Closed MatazaNz closed 3 years ago

MatazaNz commented 3 years ago

Part of the guide or code sample the question is about https://discordjs.guide/command-handling/dynamic-commands.htmll#dynamically-executing-commands

Question Using the ESLint rules, if the args parameter in the command export is unused, it will display an error, due to the no-unused-vars rule. I like this rule, as it keeps my code clean. However, with the way that the code is structured in this chapter of the guide, it seems to require both the command and args parameters. If I had any commands that did not require any arguments, how would I potentially change this? I presume I can't remove the args parameter from the exported command, as that would throw an exception at runtime. If I leave the args parameter, I have to add an ugly looking if (!args.length){ }, just so it gets registered as being used.

monbrey commented 3 years ago

You can remove args from the command run/execute function, it won't throw an error unless you then try to access it within that function while it's no longer defined. Since you're dealing with an unused var eslint rule, I'd assume that's not a problem.

Passing (message, args) to a function that only accepts (message) will just result in the second param being dropped.

I recommend you join the Discord server to get assistance with any future questions.

MatazaNz commented 3 years ago

Cheers! My apologies for the (probably) silly question. I've had experience with JavaScript, but my main background is in languages like C# and Java, so I was not actually aware that JS behaved like that. I'm programmed to think that if it's being called with 2 parameters, it must have 2 parameters in the method signature.

I'll be joining into that server, definitely.