Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

Is Node's asynchronous behavior affecting my code?

I work with Javascript on a daily basis as part of my job and it's only in the last week I've started using node.js. As a result, I'm pretty much writing code as I would with Javascript and I think that might be causing me a problem.

I have some pretty simple code. It's a twitch bot using tmi.js.
My code is this:

Code
var addPointMatch = /(?:!addpoints {1})([\w\d]{1,15}) (\d{1,4})/ig;
client.on("chat", function(channel, user, message, self) {
if (addPointMatch.test(message) == true) {
    console.log(addPointMatch.exec(message));
    var result = addPointMatch.exec(message);
    client.action("snitt", result[1] + " is being given points");
    }
});

In short, it's an event which triggers whenever a message is sent in the twitch chat. It then checks if it matches a regular expression which requires it to look something like "!addpoints [username] [value(A number)]".

If it matches the if condition then it runs the function to extract the [username] part of that expression which is held in result[1] (That's just how the .exec function works) and both prints it to the console in one instance and then my bot uses client.action which basically just sends a message in the twitch chat with my message containing the name of the username that it matched and a message. The "Snitt" parameter is just the channel name.

So thee interesting things are happening.
1) If I execute the code as I've pasted it above, it won't ALWAYS print. It will generally print most of the time but it occasionally skips one or two outputs. If I use a simpler function that just listens for a message and then spits out an output then it will catch it pretty much 100% of the time.

2) The console.log which SHOULD pick out the username in the string "!addpoints Snitt 1000" logs as "null".

3) Removing the console.log function makes this code completely break and give the following error:

Code
[09:22pm] info: [#snitt] <snitt>: !addpoints snitt 100
C:\Users\Snitt\bot\app.js:36
        client.action("snitt", result[1] + " is being given points");
                                     ^
TypeError: Cannot read property '1' of null
    at client.

It seems to be something down to Node's asynchronous abilities but I'm not 100% sure myself and I'm unsure what I should do to fix it.

Thanks in advance for your help!
I don't think this has anything to do with Node being async. The idea that it is just skipping messages at the layer of the node app seems improbable. Are you sure that regex is working correctly?

Try this, it will give you more insight if a message is skipped:
Code
var addPointMatch = /(?:!addpoints {1})([\w\d]{1,15}) (\d{1,4})/ig;

client.on('chat', function(channel, user, message, self) {
    console.log('Received Message: ' + message);

    if(addPointMatch.test(message)) {
        console.log('Message is a command!');
        var result = addPointMatch.exec(message);
        console.log('Result: ' + result);
        client.action('snitt', result[1] + ' is being given points');
    }
});


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by WntdArtJul 25, 2022 09:00 PM 0 172 Jul 25, 2022 09:00 PM
Last Post: WntdArt
[Javascript]  by PyrodenMar 12, 2019 02:20 AM 4 310 Mar 12, 2019 02:20 AM
Last Post: Fallenour
[Javascript]  by uint32Aug 31, 2017 03:32 AM 5 265 Aug 31, 2017 03:32 AM
Last Post: th3s3cr3t0n3