How can i get nightbot to send custom unique messages?

More specifically, i have a command “!Lurk” which counts how many times the user has lurked, and states how many times they have used it in chat the chat. i’m looking to see if there’s a way to send a unique message when the specific user hits a certain number of commands used, such as the number meeting a criteria of 25 minimum uses, nightbot would then state “Insert has lurked for * times, so sneaky”.

1 Like

Hey @oneXJohn!

Basically you want to check the amount of times the command was called by a user:

if (amountOfTimesCalled >= 25) 'special message';

If you’re not sure how to implement it, this will heavily depend on how your !lurk command is currently built, can you share the code you’re currently using here? Add four spaces on a new line before pasting the code so the forum formats it properly. Make sure to redact any eventual API keys.

Hi, thank you for the response. currently i’m using code that i got from elsewhere in this forum, but if i need to re-build it throughout this i won’t mind at all. i’ve blurred out both my token things since the guy i got the code from said not to give it out, when you encounter the * spam that’s what that is.

$(eval C=$(urlfetch json https://twitch.center/customapi/quote/list?token=*******&no_id=1).split(,);A=$(urlfetch https://twitch.center/customapi/addquote?token=**************&data=$(user),);x=0;for(i=0;i<C.length-1;i++){if(C[i]==$(user)){x++}};$(user) Has lurked ${x+1} times.)

i’m hoping this isn’t too difficult for you, i don’t mean to inconvenience you. and thank you for your time!

Okay, I see, thank you.
Since you need to add one to x to display the correct value, we’ll test on 24 instead of 25.
All you have to do is to replace the last part of your code:

`$(user) Has lurked ${x+1} times.`

With the following, if you want no message to be sent below 25:

x >= 24 ? `$(user) has lurked ${x + 1} times, so sneaky!` : ' ';

Or with the following if you want to keep the regular message under 25, and then have the extra bit of text starting from 25:

`$(user) has lurked ${x + 1} times${x >= 24 ? ', so sneaky!' : '.'}`;

That should do the trick.

Well, i’ve tried this, but it ended up not working for me somehow. and now trying to change it back to the original command, it’s still broken and comes up with the error messages “Unexpected identifier”, "Unexpected token ‘.’ “, " Invalid or unexpected token”.

I fixed the original command, now seeing about where i went wrong implementing your code.

What i’m seeing is that the command breaks every time i alter the original text. i’m not sure what my next step should be. could you possibly show me how you would build the command exactly?.

Sure thing!
If I simply update the code as I described:

$(eval C=`$(urlfetch json https://twitch.center/customapi/quote/list?token=*******&no_id=1)`.split(`,`);A=`$(urlfetch https://twitch.center/customapi/addquote?token=**************&data=$(user),)`;x=0;for(i=0;i<C.length-1;i++){if(C[i]==`$(user)`){x++}};`$(user) has lurked ${x + 1} times${x >= 24 ? ', so sneaky!' : '.'}`;)

But I took the opportunity to improve it a bit:

$(eval const list = '$(urlfetch json https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN&no_id=1)'.split(','); list.pop(); const add = '$(urlfetch https://twitch.center/customapi/addquote?token=PRIVATE_TOKEN&data=$(user),)'; let x = 0; for (let i = 0; i < list.length; i += 1) { if (list[i] === '$(user)') x += 1 } if (add.includes('Successfully added')) { x += 1; `$(user) has lurked ${x} times${ x >= 25 ? ', so sneaky!' : '.' }`; } else { 'There was an error, try again later'; })
  • renamed the variables,
  • added an extra test to make sure the username is added to the list,
  • used list.pop() to remove the last item of the list, which would always be an empty string, and that way I can use i < list.length instead of i < list.length - 1.

I think it makes the code a bit easier to read.

i appreciate the thought you’re putting in to being helpful, and the fact you’ve improved the code. unfortunately i’m still getting an error back "Unexpected token ‘if’ ". i don’t know what to do in this situation, and i have no idea how any of the coding works. so i’m sorry to say but i’m depending a lot on you here ;-; thank you for your time, and have a good day

I’m so sorry, that’s my fault, I tried to clarify the code a bit too much, and for a minute I forgot what’s allowed and what isn’t in an expression in a template literal, I fixed the code above, it should work now.

this works perfectly now, my last question before i get out of your hair is, how would i add more messages for different milestones?. i appreciate your help greatly, have a nice thanksgiving.

Hmm, well, since we’re getting really close to the 500 characters limits, we’ll have to rewrite the command in a smarter way, haha!

Here’s my suggestion:
You’ll need a Pastebin account to be able to edit your command in the future, and when you save your paste, make sure it’s not saved as Private otherwise Nightbot won’t be able to access it.

Here’s the command, don’t forget to replace the PASTE_ID field with your paste ID after you saved it:

$(eval const user = '$(user)'; const list = '$(urlfetch json https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN&no_id=1)'.split(','); const add = '$(urlfetch https://twitch.center/customapi/addquote?token=PRIVATE_TOKEN&data=$(user),)'; $(urlfetch json https://pastebin.com/raw/PASTE_ID))

Then, in your paste, have the following code:

const x = list.filter(item => item === user).length + 1;

if (add.includes('Successfully added')) {
  `${user} has lurked ${x} times${(x =>
    x >= 50 ? ', masterful!' :
    x >= 25 ? ', so sneaky!' :
    '.'
  )(x)}`;
} else { 'There was an error, try again later'; }

:warning: Put the numbers in decreasing order, highest number always first, smallest one always last, as ternary operators return as soon as a condition is true.


I found a way to make the code even more efficient and simpler, haha! I replaced:

let x = 0;
for (let i = 0; i < list.length; i += 1) {
  if (list[i] === '$(user)') x += 1
}

with a single line:

const x = list.filter(item => item === user).length;

In the actual code I added a +1 to account for the latest list add.

I can smell the finish line on this one, and it looks really simple to add new custom messages. but i’m getting back an error saying "Unexpected token ‘<’ " again, i am so sorry for dragging this on, i seriously have no clue how to make anything like this, and your help is really appreciated!.

There is an invalid character at the end )
Replace the last line with this ↓

} else { 'There was an error, try again later'; }
1 Like

@Bigyan_subba spotted the mistake! I kept an extra parenthesis by mistake after copy/pasting and editing the bit of code I previously wrote. Fixed the code above for future readers.

i’ve corrected the previous mistake and i’m still getting the same error.

I have no issue making it work, all I did was replace the fields and update the Pastebin code a bit:
20231126121042

So the issue is on your end, I understand that the error is linked to the < token, which I haven’t put anywhere in the code I provided, I think you’ve made a mistake in the implementation of your message options in your Pastebin code.

yes, that was exactly it! it was a new tool to me, so it took me a minute to figure out, but i think i have everything here. and a bit more experience under my belt. thank you so much for your patience and help with this!

2 Likes

Glad you figured it out, and it was a pleasure, don’t hesitate to come back if you need help again in the future!

this is all fantastic, but i feel compelled to point out the fact that this command can only function properly 2k times, as the quote system u r using to save the names has a 2k entry limit… if u rearrange the code a bit and use aliased commands u can make it function any number of times for up to 2k users (provided the username and number of uses per user doesn’t exceed 399 characters each)

1 Like