0

I'm trying to change a bit of Javascript for a Twitch Bot. This piece of code looks for a request that includes the text "(points)" and then replaces that text with the actual points the user has. Currently it's taking the loyalty points of the user and posting them directly into the chat without adding commas to every set of three digits (e.g. 1,000,000 is being posted as 1000000.)

From default, the code looks like this:

if (message.match(/\(points\)/g)) {
    message = $.replace(message, '(points)', $.getUserPoints(event.getSender()));
}

I understand that this is taking "(points)" from a message, and then using a function getUserPoints to replace it with the number of points the requesting user has.

What I've tried is this, it may be close, it may be no where near, I'm still learning Javascript so please forgive me.

if (message.match(/\(points\)/g)) {
    var retrievedPoints = $.getUserPoints(event.getSender());
    retrievedPoints.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    message = $.replace(message, '(points)', retrievedPoints);
}

From my point of view I'm taking the getUserPoints function call out of the replace function, declaring it as variable retrievedPoints and then performing the regex on this new variable before replacing "(points)" in the original message with this new, comma-fied points text.

This is passing the bot's Javascript error checking (the bot won't run if it detects any error in the Javascript code) but it's still returning "1000000" instead of "1,000,000" like I'd like it to.

I know my .toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); is correct as this bot has several ways of pulling the same data. This piece of code in particular is from the "customCommands.js" file for user's customized commands, there is another file called "pointsSystem.js" which I've managed to sort out with the above code, but this one I'm struggling on. The other file used a simple return statement, so it was just a case of tacking on the above code.

I don't think it's relevant, but it may help, the function getUserPoints appears to be this:

function getUserPoints(username) {
    return ($.inidb.exists('points', username.toLowerCase()) ? parseInt($.inidb.get('points', username.toLowerCase())) : 0);
};
radiocaf
  • 151
  • 3
  • 12
  • 2
    The variable isn't changed in place, you need to use the returned result: `retrievedPoints = retrievedPoints.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");` (exactly like you did with `message` in the next line) –  Aug 15 '19 at 09:31
  • @ChrisG so remove the the second line and change the third line to be ```message.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") = $.replace(message, '(points)', retrievedPoints);``` ? – radiocaf Aug 15 '19 at 09:37
  • 1
    No, you can never have something other than just a variable to the left of the assignment operator `=`. You need this: https://pastebin.com/GYrhR536 (or just `message = $.replace(message, '(points)', $.getUserPoints(event.getSender()).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));`) –  Aug 15 '19 at 09:40
  • Oh I see now, thank you so much. I was trying to change a variable in place as you said. I even see how to showed me this in your first comment, thank you though, @ChrisG As I've said in the question I'm still new to Javascript and it doesn't seem to be too forgiving. EDIT: it worked perfectly. Thank you again! – radiocaf Aug 15 '19 at 09:46

0 Answers0