Adding a variable to a urlfetch

I’m using a custom API site that stores quotes and I would like to add to the site using data from a variable.
I have something like:
$(eval
var s=“the info to put on the site”;
s)
then I want to be able to do:
$(urlfetch someURL.com/ + s)

How could I accomplish this?

Nest the eval inside the urlfetch

$(urlfetch http://someURL.com/$(eval var s="the info to put on the site"; encodeURIComponent(s)))

Yes, I saw this solution on another post. However, there’s a few reasons why I can’t get that to work.
Here’s all of my code (basically):
// code
!commands edit !setWins -ul=moderator -a=!commands edit !wins -ul=everyone $(eval
var q="$(querystring)";
var s=“0 wins today”;
var throwaway=“ree”;
if(""!=q.trim()) {
var wins=q.split("%2C");
if(wins.length>0)
s=wins.length+" wins (" + wins.join(" kills, “) + " kills)”;

// stuck here
throwaway="$(urlfetch deleteAllQuotesURL.com)";
throwaway="$(urlfetch addAQuote.com/data= + s)";

} s)

Because I’m returning s from eval and trying to use it in the link, I don’t think that your exact solution can help.

Here is a solution.

Replace PUBLICTOKEN with your public token. This is the shorter token.
Replace PRIVATETOKEN with your private token. This is the longer token.

Edit your !wins command to urlfetch the win count instead of being static text:

!editcom !wins $(eval r=decodeURIComponent("$(urlfetch json https://twitch.center/customapi/quote?token=PUBLICTOKEN&data=1&no_id=1)");r=="There are no quotes added"?"Wins not yet set. Use !setwins":r)

Edit your !setwins command to calculate the URLs to urlfetch. It will be an alias to a new command, _!setwins.

!editcom !setwins -a=_!setwins -ul=moderator $(eval p=(c,n)=>`${c} ${n}${c==1?"":"s"}`;k=decodeURIComponent("$(querystring)").match(/\d+/g);"delquote?token=PRIVATETOKEN&clear=1 addquote?token=PRIVATETOKEN&data="+encodeURIComponent(k?`${p(k.length,"win")} (${k.map(c=>p(c,"kill")).join(", ")})`:"0 wins today"))

Add a new command _!setwins to urlfetch the clear and add URLs:

!addcom _!setwins -ul=moderator $(eval d=decodeURIComponent;cR="$(urlfetch json https://twitch.center/customapi/$(1))";aR="$(urlfetch json https://twitch.center/customapi/$(2))";if(m=d("$(querystring)").match(/addquote\?token=(.+?)&data=(.+)/)){if(m[1]=="PRIVATETOKEN"){if(cR=="All entries have been deleted"&&aR=="Successfully added entry #1"){d(m[2])}else{`Error replacing quote: ${cR}. ${aR}`.slice(0,400)}}else"Incorrect token"}else"Use !setwins")
1 Like

Thank you for all of your help! I managed to make this work with only two commands.
I used the code for !wins provided by @am_1 and edited my !setwins class to add to the API site rather than use an alias.
!commands edit !setWins -ul=moderator $(urlfetch $(eval
var q="$(querystring)";
var s=“0 wins today”;
var throwaway=“ree”;
var url=“https://twitch.center/customapi/addquote?token=CODE&data=”;
if(""!=q.trim()) {
var wins=q.split("%2C");
if(wins.length>0)
s=wins.length+" wins (" + wins.join(" kills, “) + " kills)”;
url+=s;
throwaway="$(urlfetch https://twitch.center/customapi/delquote?
token=CODE&clear=1)";
} url))
I started making changes to this command so that I could also implement an !addWin command that can edit the quote stored. Tihs way, I can add a win without having to keep track of previous wins.
The site that I’m using to store information is here.
EDIT: The commands that am_1 made are much better than mine. I just wanted to try and create them myself.

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