$(1) $(2) and $(query) help

Hi,

I have been trying to get a little !define command working pulling words from chat (discord) and returning the results from an API.

i can code the api search (Curl in php) to either accept 1 string with all words concatenated

i.e.

$term = $_GET[“t”];
define.php?t=[input from nightbot]

or i can code the api search to accept 2 strings and concatenate them myself

i.e.

$term = $_GET[“t”];
$term .= $_GET[“t2”];
define.php?t=[input from nightbot]&t2=[2nd input from nightbot]

the issue i have is that i want the command to work if there is only 1 argument given in discord.

for example i can get “!define arg1 arg2” to work but !define arg1" does not work

what value is passed by “$(2)” if nothing is entered? something must be being passed when it is empty as it breaks the command.

i have tried

$(urlfetch http://websiteurl/define.php?t=$(eval ‘$(1)’ + ‘$(2)’))
to concatenate on nightbots end which works if $(1) and $(2) are given but not if only $(1) is defined

and

$(urlfetch http://websiteurl/define.php?t=$(1)&t2=$(2))
to concatenate on my end, which again works if $(1) and $(2) are given but not if only $(1) is defined

basically i want to find out why it doesnt work if $(2) is undefined and if there is a way to get around this

kind regards
mark

Hiya, I would suggest using the $(querystring) variable. Info: https://docs.nightbot.tv/commands/variables/querystring

This will be text the user typed after the command, url-encoded. In your script you can check how many arguments are provided by using for example explode on the querystring.

For your other question, if a variable like $(2) is not entered it will be replaced by a string “null”.

Thanks for your help @xgerhard i have worked out a solution.

ill post it below if anyone would like to know what i did if they run into similar issues

i pass the it to php as $(querystring)

$(urlfetch http://api.websiteurl.com/define.php?t=$(querystring))

then as $_get runs it through urldecode() i just need to remove the spaces as follows to enter it into Curl

$termfromurl = $_GET[“t”];
$term = str_replace(’ ', ‘’, $termfromurl);

$curl = curl_init($term);

thanks again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.