Skip to content

Instantly share code, notes, and snippets.

@WouterSchols
Last active February 29, 2024 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WouterSchols/81310133f6f8585fd109216bf421529e to your computer and use it in GitHub Desktop.
Save WouterSchols/81310133f6f8585fd109216bf421529e to your computer and use it in GitHub Desktop.
A collection of nightbot commands for the twitch chess community
# Creates a new command to display a players rapid elo
# usage: !elo username (!elo hikaru)
# response: hikaru current rating is 2594 and highest rating is 2829
!addcom !elo
$(eval
const api = $(urlfetch json https://api.chess.com/pub/player/$(query)/stats);
"$(query) current rating is "+ api[`chess_rapid`][`last`][`rating`] +
" and highest rating is " + api[`chess_rapid`][`best`][`rating`]
)
# Creates a new command to display the amound of wins, losses and draws replace NAME with a chess.com username
# Please note that the command is capped at 500 char so indentation had to be removed when making the command
# usage !score
# response Today USER has 1 wins, 0 draws and 1 losses
!addcom !score
$(eval
const api=$(urlfetch json https://api.chess.com/pub/player/NAME/games/2024/$(time US/Eastern "MM"));
const day = $(time US/Eastern "D");
win=0;
loss=0;
draw=0;
for (game of api[`games`]) {
if(day == new Date(game[`end_time`]*1000).getDate()) {
if(game[`pgn`].slice(-2,-1)==2){
draw++
}else if(game[`pgn`].slice(-2,-1)==(game[`white`][`username`]=="NAME")){
loss++
}else{win++}
}}
"Today NAME has "+win+" wins, "+draw+" draws and "+loss+" losses"
)
@bobk999
Copy link

bobk999 commented Feb 25, 2024

Hello, and thank you so much, these are super helpful! I'm trying to make an !elo command that shows rapid, blitz and bullet ratings. I don't know anything about code, but using your !elo command I was able to almost make it work. This was my input
$(eval const api = $(urlfetch json https://api.chess.com/pub/player/$(query)/stats); "$(query) Rapid: "+ api[chess_rapid][last][rating] )$(eval const api = $(urlfetch json https://api.chess.com/pub/player/$(query)/stats); "$(query) Blitz: "+ api[chess_blitz][last][rating] )$(eval const api = $(urlfetch json https://api.chess.com/pub/player/$(query)/stats); "$(query) Bullet: "+ api[chess_bullet][last][rating] )
and this is the output
hikaru Rapid: 2769hikaru Blitz: 3241hikaru Bullet: 3282
Is there any way to remove the name, or have it only show up once before the ratings?

@WouterSchols
Copy link
Author

Tldr remove $(query) before blitz and bullet.

Longer answer $(query) is a variable which contains the input given to the command. In this case a chess.com user name. First, the script creates a new variable $(api). $(api) is filled with some data from chess.com based on the $(query) user id. This is just a simple page which contains a bunch of user data. You could even open it in a browser (https://api.chess.com/pub/player/hikaru/stats).

The script reads the right data from the website and prints it. Currenrly, you fetch the same data 3 times. You could rewrite the command to only get it once and read it 3 times.

@bobk999
Copy link

bobk999 commented Feb 29, 2024

Awesome! It works like a charm now. Thank you so much for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment