How can I create more commands like this one?

Hello team
I have this command

Summary

$(eval var str=decodeURIComponent($(urlfetch json https://api.chess.com/pub/player/$(touser)/stats)); if(str==“Remote Server Returned Code 404”){ ‘:x:Not Found’; }else{ bu=JSON.parse(str).puzzle_rush.best ? JSON.parse(str).puzzle_rush.best.score : ‘None’; ‘:heavy_check_mark: $(touser) Puzzle Rush Record is 🢂’+bu})🢀 on :chess_pawn:chess.com​:chess_pawn:

which is related to a chess page, this commands sends the best record in a certain part of the game called “puzzle rush”, I would like to know how to create more like this, for other types of games in the same chess page.

I have some other commands related as well which provide the specific rating a player has in different time controls chess games ( in 1 minute chess, 3 minute chess and 10 minute chess) I can send them too if necessary

Hey @Clauwasaki!

I understand that you want to create similar commands based on the API; it returns JSON so it’s quite easy to set up, however, since your request is very broad, the best thing I can offer you is a quick tutorial on how to access data in a JSON.

Hi Emily, I see, I know the specific numbers but I don’t know where to send them afterwards, do I need something to send the data to? like a page or something

I’m not sure I understand your issue, sorry, but hopefully the following example will help you…

Let’s imagine the API responds with the following JSON:

{
  "firstName": "Edyth",
  "lastName": "Owbridge",
  "pets": [
    "Dove",
    "Palm squirrel",
    "Stork",
    "African jacana",
    "Badger"
  ],
  "friends": [
    "Dasya",
    "Joni",
    "Fabian",
    "Tarah",
    "Tonie",
    "Quentin",
    "Carce",
    "Korella",
    "Cherie",
    "Fallon"
  ]
}

And you want to generate the following sentence:

FIRST_NAME has PETS_AMOUNT pets, one of which is a RANDOM_PET, and their best friend is FRIEND_NUMBER_4.

Then you command will look like this:

$(eval data = $(urlfetch json API_URL); `${data.firstName} has ${data.pets.length} pets, one of which is a ${data.pets[Math.floor(Math.random() * data.pets.length)]}, and their best friend is ${data.friends[3]}.`;)

If you need further help, I’ll need an example of the data output and an example of the sentence you want to generate, like I showed you, as I can’t help blindfolded.

Sorry if it’s broad. I bring this pics to show what I would like to know if it’s possible to make. So in the command I showed in the first comment it sends this info

and I would like to know if it’s possible to get the info let’s say from this part

so my 2 questions are: What info do I need? and What part of the command do I have to change?

I can’t tell you blindfolded, your screenshots help, but I’m still missing the most important part, something similar to this:

You can get it by replacing $(touser) in the URL with your username on chess.com, I can’t know it if you don’t tell me your username, and sometimes people don’t want to give that info publicly, that’s why I offered you to show me an example of what the data looks like in JSON instead.

Anyway, when I go to the following URL:

https://api.chess.com/pub/player/c_l_a_u_w_a_s_a_k_i/stats

I get the following response:

{
  "chess_bullet":{
    "last":{
      "rating":1906,
      "date":1683703686,
      "rd":33
    },
    "best":{
      "rating":1919,
      "date":1680501473,
      "game":"https://www.chess.com/game/live/GAME_ID"
    },
    "record":{
      "win":187,
      "loss":131,
      "draw":19
    }
  },
  "chess_blitz":{
    "last":{
      "rating":1907,
      "date":1683871960,
      "rd":17
    },
    "best":{
      "rating":1907,
      "date":1683866871,
      "game":"https://www.chess.com/game/live/GAME_ID"
    },
    "record":{
      "win":604,
      "loss":653,
      "draw":100
    }
  },
  "tactics":{
    "highest":{
      "rating":2694,
      "date":1682118793
    },
    "lowest":{
      "rating":400,
      "date":1680026547
    }
  },
  "puzzle_rush":{
    "best":{
      "total_attempts":30,
      "score":27
    },
    "daily":{
      "total_attempts":28,
      "score":27
    }
  }
}

That’s what I was asking for.

Now, after comparing the data displayed on the website, and the data available on the API endpoint you provided, it looks like it doesn’t include the data for Puzzle Rush → Best 5 min.

And looking at their API documentation I couldn’t find a public endpoint that would have that data.

I see, well then. it can always be made manually and edited as it changes. Thanks a lot for the help!

1 Like

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