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);
};