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".
How can i get nightbot to send custom unique messages?
34 replies
Hey @Calm Orangutan!
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:
js`$(user) Has lurked ${x+1} times.`
With the following, if you want no message to be sent below 25:
jsx >= 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:
js`$(user) has lurked ${x + 1} times${x >= 24 ? ', so sneaky!' : '.'}`;
That should do the trick.
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 usei < list.lengthinstead ofi < 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
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:
jsconst 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'; }
⚠️ 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:
jslet x = 0;for (let i = 0; i < list.length; i += 1) {if (list[i] === '$(user)') x += 1}
with a single line:
jsconst x = list.filter(item => item === user).length;
In the actual code I added a +1 to account for the latest list add.
I have no issue making it work, all I did was replace the fields and update the Pastebin code a bit:
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.

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)
sure thing ^^
before we begin the updated functionality, do u wish to save the current count u already have saved in there?
if u do, there r a couple of options... u could write down the totals for each name manually... or create a new token set for the quote system to store the new data... but either way u'll need an extra command to retrieve the data in a usable format... well, or u could go super analog and just pull up the quote list and count them all yourself...
if u don't, u can simply clear the data and start over
but lemme know on that part, meanwhile i'll get started on the updated functionality ^^
ok, so for the first command in the aliased set for the new functionality...
js$(eval a="$(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=$(user))".split(` `);if(a[1]==0){a[2]=1};a[0]==`0.`||a[0]==`No`||a[0]==`There`?`0 $(user) 1 add`:`${a[0].replace(`.`,``)} $(user) ${(a[2]*1)+1} edit`)
and then for the second command in the aliased set...
js$(eval a=`$(urlfetch https://twitch.center/customapi/$(4)quote?token=PRIVATE_TOKEN&data=$(query))`;x=`$(3)`*1;x&&a.includes(`Success`)?`$(2) has lurked ${x} time${x==1?``:`s`}${(x=>x>=50?`, masterful!`:x>=25?`, so sneaky!`:`.`)(x)}`:`Error, try again later`)
don't forget to change PUBLIC_TOKEN to the 8 digit token and PRIVATE_TOKEN to the 16 digit token for an empty quote list
i kept the "masterful!" along with the "sneaky!" from the example above... it's not as pretty cause i shortened a few things to give more room in the command to add other options beyond sneaky and masterful if u wish, but the same rules apply from above ^^
also, @Polar Cat i'm quite curious y it needs the parenthesis around the function and again with the variable to make it work within the curly braces (i've always just defined the function earlier and called it from within the curly braces)
Stellar Raccoon said:
also, @Polar Cat i’m quite curious y it needs the parenthesis around the function and again with the variable to make it work within the curly braces (i’ve always just defined the function earlier and called it from within the curly braces)
Are you referring to this bit of code?
Polar Cat said:
```
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'; }
```
That's the syntax when you want to write an entire function within template literals, instead of the function name, you have the entire function, the first x in the function is the name of the variable for the function, but the last x is the const that we feed to the function.
I could rewrite the code a bit so it's easier to understand:
diff- const x = list.filter(item => item === user).length + 1;+ const usernameAmount = list.filter(item => item === user).length + 1;if (add.includes('Successfully added')) {- `${user} has lurked ${x} times${(x =>+ `${user} has lurked ${usernameAmount} times${(x =>x >= 50 ? ', masterful!' :x >= 25 ? ', so sneaky!' :'.'- )(x)}`;+ )(usernameAmount)}`;} else { 'There was an error, try again later'; }
But of course, I could have extracted the function outside too, which may help you understand the syntax:
const x = list.filter(item => item === user).length + 1;const extraMessage = (x) => {if (x >= 50) return ', masterful!';if (x >= 25) return ', so sneaky!';return '.';}if (add.includes('Successfully added')) {
Ignore my long message, I was explaining something to @member
If you want to purge all your current data, go to the following URL:
https://twitch.center/customapi/delquote?token=PRIVATE_TOKEN&clear=1
That's what @Stellar Raccoon was suggesting you do:
/t/how-can-i-get-nightbot-to-send-custom-unique-messages/1534651284068634786#…
Yes, the maintainer of the quote API started limiting the amount of quotes because too many people were using it as a database, running a server isn't free, I believe you can request the limit to be lifted once you reach it, but I doubt it'll be approved for people who use it as a database.
I realize I'm part of the problem as I was promoting the use of the quote API as a database by creating commands for people which used that API in this way, so I stopped doing that, I don't even help anymore when people have issues with the quote API if they're using it as a database (which wasn't your case: your issue wasn't with the API), besides I believe we—volunteers—have created enough commands that people should be able to figure out how to create their own, I think we've covered the most common requests people would come up with, people just have to search the forum now.
Didn't find your answer? The community is on Discord.
Ask on Discord