A custom greeting command for YT chat using $(eval) bug

Here’s a little thing I want to do in my chat: when someone says a greeting word (hi, hello, gday, sup etc…) I want Nightbot to react like this: if the query is nothing, say ‘Hello there, welcome to the stream!’; if the query is ‘guys’, say ‘Hello $(user), welcome to the stream!’; and is the query is anything else, say ‘Hello $(query), welcome to the stream!’. The problem is, the eval function prints out ‘Right-hand side of ‘incetanceof’ is not an object’. If someone could find what causes it or a way to fix it, please post about it.

My output code is: Hello $(eval const r = [`$(query)`,`there`,`$(user)`]; r[isNaN($(query).length/$(query).length)+($(query)==’guys’)*2];), welcome to the stream!

Thanks for reading!

Hello $(eval if (`$(query)`){`$(query)`.toLowerCase() == "guys" ? `$(user)` : `$(query)`}else{"there"}), welcome to the stream!

That’s some strange JS… any explanation as to how this works? is it even JS?

It is JS. The JS part (the part inside the $(eval )) looks like this when formatted differently:

if (`$(query)`) {
    `$(query)`.toLowerCase() == "guys" ? `$(user)` : `$(query)`
} else {

Here it is with comments to explain the code:

if (`$(query)`) { // Test whether the query is not empty
    // The query is not empty
    `$(query)`.toLowerCase() == "guys" ? `$(user)` : `$(query)` // Ternary operator. The operator returns the user's name if the query is "guys" (case insensitive). Otherwise, the operator returns the query. Nightbot will print whatever the operator returns since it is the last evaluated expression.
} else {
    // The query is empty
    "there" // Nightbot will print "there" since it is the last evaluated expression

More information about the ternary operator:

i used this, but theres one thing i dont like and that is that the only way i know how to use this is to use it as a command and i named hi command hi without an exclamation point, so is there a way this could be used without putting it in a command

Nope sorry, commands can only be called in chat via the command name.

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