Help with a Nightbot command that fetches Valorant Win/Loss history for the day

Hello,

I used this website to generate the url below: Main Page | henrikdev.xyz

When I try to utilise this command in my chat:

@$(user), $(eval m='$(urlfetch json https://api.henrikdev.xyz/valorant/v1/mmr-history/na/lebronze%20james/nba2k)';r=JSON.parse(m);t=0;h=new Date().getHours();p=new Date().getDate();a=0;b=0;for(i=0;i<17;i++){d=r.data[i].date;s=d.indexOf(",",d.indexOf(",")+1);d=d.substring(s-2,s);n=r.data[i].mmr_change_to_last_game;if(h>16&&d>p){d=p;}if(p==d&&n>0){t+=n;a++;}else if(p==d){t+=n;b++;}}g="jamie is ";if(t>0){g+="UP ";}else{g+="DOWN ";}g+=t+"RR"+a+"-"+b+" ";g;)

I receive and error saying:

@cxniie , Cannot read properties of undefined (reading ā€˜dateā€™)

Iā€™m assuming thereā€™s an error in this portion of the code:

{d=r.data[i].date;s=d.indexOf(ā€œ,ā€,d.indexOf(ā€œ,ā€)+1);d=d.substring(s-2,s);n=r.data[i].mmr_change_to_last_game;if(h>16&&d>p){d=p;}

But I donā€™t know how to go about fixing it.

Hey, I rewrote your command to be more reliable:

@$(user), $(eval j=decodeURIComponent("$(querystring $(urlfetch json https://api.henrikdev.xyz/valorant/v1/mmr-history/na/lebronze%20james/nba2k))");try{e=JSON.parse(j),t=new Date,a=0,s=0,r=0;for(let{date_raw:n,mmr_change_to_last_game:o}of e.data){l=new Date(1e3*n);l.getFullYear()===t.getFullYear()&&l.getMonth()===t.getMonth()&&l.getDate()===t.getDate()&&(a+=o,o>0?s++:r++);`jamie is ${a>0?'UP':'DOWN'} ${a}RR${s}-${r}`}}catch(g){`Failed to parse game history: ${g.message}: ${j}`.slice(0,400)})

Here is the non-minified version of the eval code for reference:

Non-minified eval code
const responseJson = decodeURIComponent("$(querystring $(urlfetch json https://api.henrikdev.xyz/valorant/v1/mmr-history/na/lebronze%20james/nba2k))");
try {
  /** @type {{
    readonly data: ReadonlyArray<{
      readonly mmr_change_to_last_game: number;
      readonly date_raw: number;
    }>;
  }} */
  const response = JSON.parse(responseJson);
  const now = new Date();
  let mmrChangeToday = 0;
  let winCountToday = 0;
  let lossCountToday = 0;
  for (const {date_raw: dateUnixS, mmr_change_to_last_game: mmrChange} of response.data) {
    const date = new Date(dateUnixS * 1000);
    if (
      date.getFullYear() === now.getFullYear()
      && date.getMonth() === now.getMonth()
      && date.getDate() === now.getDate()
    ) {
      mmrChangeToday += mmrChange;

      if (mmrChange > 0) {
        winCountToday++;
      } else {
        lossCountToday++;
      }
    }
  }

  `jamie is ${mmrChangeToday > 0 ? 'UP' : 'DOWN'} ${mmrChangeToday}RR${winCountToday}-${lossCountToday}`;
} catch (e) {
  `Failed to parse game history: ${e.message}: ${responseJson}`.slice(0, 400);
}

This redone version of your command is not restricted to exactly 17 recent games, handles dates more safely, and has error reporting.

Warning: the logic for whether a game occurred today is based on the time zone of Nightbotā€™s servers.

This is because your code was trying to process exactly 17 recent games when fewer existed.

1 Like

Oh NO WAY! Thatā€™s crazy, thank you so much for putting all that time and effort into this!

Would it be possible to have Nightbot refresh the match history/rr gainloss when I go live? So that it checks every stream session and not based on a period of time?

Try this command:

@$(user), $(eval $(urlfetch json https://pastebin.com/raw/x3vGs4C9)('$(twitch $(channel) "{{uptimeLength}}")','$(twitch $(channel) "{{uptimeAt}}")',"$(querystring $(urlfetch json https://api.henrikdev.xyz/valorant/v1/mmr-history/na/lebronze%20james/nba2k))"))

This command only considers games that occurred after the currently live stream started. So if your channel is not currently live, it will return ā€œjamie is not liveā€. Iā€™d like for the command to work for games that occurred during the previous stream if the channel is not currently live, but Nightbotā€™s twitch variable does not supply information about the previous stream. Maybe someone has a custom API for this. Unsure.

Hereā€™s the code for the part of the command thatā€™s hosted on Pastebin. I had to host the majority of the commandā€™s code on Pastebin since the code was too long for Nightbot by itself. This will be useful if the Pastebin link is ever taken down.

Code
((
  /** @type {string} */ streamUptimeString,
  /** @type {string} */ streamStartDateString,
  /** @type {string} */ urlEncodedGetMmrHistoryResponseJson,
) => {
  /* streamStartDateString will be a date string even if the channel is not currently live (the date will be the current
     date). This may be a Nightbot bug. This is why streamUptimeString is needed to check whether the channel is live */
  if (/\bnot live\b/i.test(streamUptimeString)) {
    return 'jamie is not live';
  }

  const streamStartDate = new Date(streamStartDateString);
  if (Number.isNaN(streamStartDate.valueOf())) {
    return `Failed to parse stream start date: ${streamStartDateString}`.slice(0, 400);
  }

  const getMmrHistoryResponseJson = decodeURIComponent(urlEncodedGetMmrHistoryResponseJson);
  if (/^Error Connecting To Remote Server\b/i.test(getMmrHistoryResponseJson)) {
    return getMmrHistoryResponseJson;
  }

  try {
    /** @type {{
      readonly data: ReadonlyArray<{
        readonly mmr_change_to_last_game: number;
        readonly date_raw: number;
      }>;
    }} */
    const getMmrHistoryResponse = JSON.parse(getMmrHistoryResponseJson);

    let mmrChangeThisStream = 0;
    let winCountThisStream = 0;
    let lossCountThisStream = 0;
    for (const {date_raw: dateUnixS, mmr_change_to_last_game: mmrChange} of getMmrHistoryResponse.data) {
      const date = new Date(dateUnixS * 1000);
      if (date >= streamStartDate) {
        mmrChangeThisStream += mmrChange;

        if (mmrChange > 0) {
          winCountThisStream++;
        } else {
          lossCountThisStream++;
        }
      }
    }

    return `jamie is ${mmrChangeThisStream > 0 ? 'UP' : 'DOWN'} ${mmrChangeThisStream}RR${winCountThisStream}-${lossCountThisStream}`;
  } catch (e) {
    return `Failed to parse MMR history: ${e.message}: ${getMmrHistoryResponseJson}`.slice(0, 400);
  }
})

Please let me know if this is what you had in mind or if youā€™d like any help making modifications.

1 Like

I havenā€™t been able to test your new command yet, but I just want to say thank you for taking time out of your day/night to do this for me; a complete stranger. It really means a lot to me. Once again thank you c:

1 Like

Hello!

I was able to test your command today, there are some things that I noticed with the new command. It seemed to update a lot slower than the previous command you made. Is that due to Nightbot having to fetch from two URLs? It was a considerable amount of latency. I was at 2-1, and won two and lost 1, and it still registered at 2-1.

For the newer command would you be able to change the formatting to look like this?
jamie is UP 35RR. W/L: 7-4

It currently looks like
jamie is DOWN 0RR0-0

Otherwise, the addition of having it know when Iā€™m live is so nice ^^

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