Help, how can I make a command with different response depending on the input?

Asked May 25, 2022·2 replies·Last activity 4 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

Hello, does anyone know how I can create a command that works in two ways, first, that when someone does not place any input, that is, placing only the command, nightbot responds in one way. and second, that when the someone places an input followed by the command, for example a "@user", nightbot responds with a different message.

2 replies

Hey @Deft Eagle!

Funnily enough, the very first question I asked on this forum was pretty similar, here's the answer I got:
(link expired)

Basically, you want to add a test on the Nightbot variable you use in your code, but how you implement the test depends on what variable you use:

  • $(query) and $(querystring) give an empty string when no argument are fed into the command

→ so you test it that way: if ('$(query)' === '')

  • arguments like $(1) or $(8) give a null when no argument are fed into the command

→ so you test it that way: if ('$(1)' === 'null')

  • finally $(touser) always responds with something: when no argument are fed into the command it gives $(user)

→ so to test if the user of the command @'d someone: if ('$(touser)' != '$(user)')

Since $(touser) is equivalent to $(1), but doesn't require the use of $(user) in the test, I'll use the latter for the example:

$(eval if ('$(1)' === 'null') { 'MESSAGE_IF_NO_INPUT' } else { 'MESSAGE_IF_INPUT' })

or with ternary operators:

$(eval '$(1)' === 'null' ? 'MESSAGE_IF_NO_INPUT' : 'MESSAGE_IF_INPUT')