Thank you!
I’m not sure I understand how your code works, have you tested it, is it working properly? It doesn’t seem to do coherent actions. Also, you inverted the PUBLIC_TOKEN
and PRIVATE_TOKEN
in your commands, the PUBLIC_TOKEN
is 8 characters long, while the PRIVATE_TOKEN
is double that: 16.
From what I understand, you want people to be able to give slice of pizza to each others, without being able to grab them for themselves, have I got that right?
Keeping this assumption in mind, I’ll correct the code and simplify it:
• !slicegive
:
$(eval '$(touser)' != '$(user)' && !'$(twitch $(touser))'.includes('Error') ? 'PRIVATE_TOKEN&data=$(touser)' : ' ';)
Here I’m making sure that a chatter doesn’t grab a slice for themselves: '$(touser)' != '$(user)'
; and that if the Twitch username doesn’t exist, it’ll give an error later: '$(twitch $(touser))'.includes('Error')
: Nightbot’s error doesn’t say “Unknown Twitch Channel
”, but “Error: Channel not found.
”, and making the test only on the existence of the channel ignores all the other possible errors that we want to consider. I’m also simplifying the entries to the quote list — you don’t really need the tilde (~
) separator — by making it the targeted user.
• !slicegive2
(consider renaming it _slicegive
, that’s the convention we use):
$(eval a = '$(urlfetch https://twitch.center/customapi/addquote?token=$(query))'; b = '$(query)'; b.includes('PRIVATE_TOKEN') ? a.includes('Successfully added entry') ? `${b.replace('PRIVATE_TOKEN&data=', '')} just received a slice of pizza! Next one will be ready in 5 minutes! Check how many you've had with !slicecheck` : a : 'That\'s an invalid Twitch name! / You can\'t grab a slice for yourself!';)
Here you were forgetting to remove the PRIVATE_TOKEN
from your output, this is dangerous as people would be able to edit your quote list; the invalid Twitch name error was misplaced, and finally, the maximum command cooldown is 5 minutes with Nightbot, not 15.
• !slicecheck
:
$(eval u = '$(user)'.toLowerCase(); a = '$(urlfetch json https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN)'.split(/\d+\.\s/); a.shift(); m = a.map(b => b.toLowerCase()); s = m.filter(n => n === u).length; `${u} has had ${s} slices.`;)
Just a simplification to go along with the few changes I’ve done to !slicegive
.
So here’s what I’m thinking to solve your problem: since we store usernames to count how many slice each and everyone got, we can use the same process to build your leaderboard, the idea is to start from either side of the list, and try to find any matches to the username, add up how many we found, and repeat the process for each entry, if the username of one entry has already been counted, skip it.
My concern is that this has a bad Big O Notation rating, meaning the larger the list gets, the longer it’ll take to compute, and Nightbot limits the amount of computation time you get so one doesn’t use all the power of the bot for themselves, so it might occasionally time out, in which case the solution is to reset the list. I tried to mitigate that as much as possible with my code, but working with the character limitation, so it’s not perfect. Also, as mentioned previously, you’ll reach Twitch’s character limitation pretty quickly.
Anyway, here’s how I’d write the code for your !sliceboard
command:
$(eval a = '$(urlfetch json https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN)'.split(/\d+\.\s/); a.shift(); m = a.map(b => b.toLowerCase()); u = Array.from(new Set(m).values()); r = []; u.forEach(v => r.push({ 'u': v, 'a': m.filter(n => n === v).length })); r.sort((s, t) => t.a - s.a); o = r.map(s => `${s.u}: ${s.a}`); o.join(' | ').slice(0, 400);)
First we grab the entire list in an array, then we make all the usernames lowercase in case there are variations in how people enter the usernames, then we create a list of unique usernames, and finally we count how many times an username appears in the full list, after that we rank them from highest to lowest, and then we build the response.