# Edge Animate Sub alert sometimes doesn't trigger

Hi,
Although most of my time now is made on other projects, I’ve noticed that since the new re-sub changes, my sub alert occasionally doesn’t trigger.
It was created in Adobe Edge Animate, and it was made for a partnered channel for use in the CLR Browser. The alerter used to work properly every time. Now though, it occasionally doesn’t trigger. It seems very random, but is mostly related to re-subs with messages attached. These do trigger, but seem to be the only ones that occasionally don’t.

var nick = 'justinfan1234';
var pass = 'twitch';
var socket = new WebSocket("ws://irc-ws.chat.twitch.tv:80");
socket.onopen = function (event) {

socket.send('NICK ' + nick);
socket.send('PASS ' + pass);
socket.send('JOIN #' + channel);

socket.send('CAP REQ :twitch.tv/commands');
socket.send('CAP REQ :twitch.tv/tags');
socket.send('CAP REQ :twitch.tv/membership');

};


I’ll list what’s inside the socket.onMessage function below. Because of how inefficient the code is, and most of it is to do with Edge animate, I’ll only list what’s related to the twitch messages.

var message = event.data.substring(event.data, event.data.indexOf('\r'));
if (message.indexOf('user-type= :tmi.twitch.tv USERNOTICE') >= 0) {
var userNotice = message.split(";");
if (userNotice[6].indexOf('resub') >= 0) {
var reSubName = userNotice[4].substring(userNotice[4].indexOf("=") + 1);
var reSubAmount = userNotice[7].substring(userNotice[7].indexOf("=") + 1);
var usertypeMsg = userNotice[13].substring(userNotice[13].indexOf("=") + 16);
var reSubMessage = usertypeMsg.substring(usertypeMsg.indexOf("USERNOTICE #" + channel + " :") + 23);
AdobeEdge.getComposition("subAnim").getStage().$("months-underline").show(); AdobeEdge.getComposition("subAnim").getStage().$("Text2").html(reSubName);

if (reSubMessage != null && reSubMessage != "") {
AdobeEdge.getComposition("subAnim").getStage().$("months").html("for " + reSubAmount + " months - " + reSubMessage); } else { AdobeEdge.getComposition("subAnim").getStage().$("months").html("for " + reSubAmount + " months!");
}
}


I’m still unaware of what’s causing it, and because of how random it is, I’m still yet to catch an error (because I only remember to open an instance of it in the browser after I’ve been told it’s not working.

From what I can see in the browser, it occasionally fails to connect (with no errors, as if it’s not trying), so i have to refresh. Again, I’m yet to catch the problem as it occurs, so I don’t know if I’m getting errors from this.

Thanks,
Mike

My best guess is this line:

if (userNotice[6].indexOf('resub') >= 0)


You’re already inside of a USERNOTICE, so you know it is a resub. Try removing that and see if it is more consistent.

1 Like

Thanks, I’ll give that a go and report back! Since it’s completely random, I probably won’t know straight away if it’s fixed or not, but yeah I’ll keep you up-to-date!

@DallasNChains So I tried that change, and after logging into the stream today, had reports that someone’s alert didn’t go off. This time, they didn’t attach a message to it, so that’s eliminated my original theory that messages were breaking it.

However, she DID mention that the bot didn’t say anything. We have a bot, called Cray0nbot, with an automated thank-you message when someone re-subs or subscribes for the first time. I believe the streamer is using Ankhbot for that, although that’s unimportant. She reported that the bot didn’t say anything at all either, which usually only happens when Twitch are having problems as far as I’m aware.

EDIT -
So today turned out to be my sub anniversary day, so I threw it up (without a message) to get the raw message from Twitch, and here it is. I don’t see anything with it that’s affecting my if statements from detecting it. It saw the message, and simply said “not a sub”. (I added the square bracket numbers as an indicator for my split command)

         [0] @badges=moderator/1,subscriber/1,bits/100;
[1] color=#570080;
[3] emotes=;
[5] mod=1;
[6] msg-id=resub;
[7] msg-param-months=19;
[8] room-id=32775646;
[9] subscriber=1;
[11] turbo=0;
[12] user-id=49705491;
[13] user-type=mod :tmi.twitch.tv USERNOTICE #reninsane

You should really write a real IRC and IRCv3 tags parser that splits messages based on the protocol.

For example you have this to check for a resub message:
if (message.indexOf('user-type= :tmi.twitch.tv USERNOTICE') >= 0) {

What if the user-type tag isn’t empty? What is there is another tag added behind it or the order of the tags is different? What if a user simply sends a regular chat message that contains that string? Sure, that last one is unlikely, but it can still happen. The issue with doing it like this is that it can break really easily.

The same applies for how you read the tags. It is not guaranteed that for example login will always stay the 5th tag. It is also mentioned in the IRCv3 tags spec that the order of tags must not matter.

Writing a very basic IRC parser isn’t hard, or maybe there even are libraries for that already you can use. Is that JavaScript?

Literally just as you commented, I found the problem all this time… I never clicked on that it was ONLY moderators that it wasn’t working for. Now I see it.

And yes, it’s Javascript. I don’t know how I’d install npm packages to an Edge Animate project running on an Apache2 server, but yes I agree it’s a bad way of working it.

If you can’t get a library to work, then writing a parser yourself isn’t too difficult. The basic structure of an IRC message is:

@[tags] :[prefix] [command] [parameters] :trailing

Tags, prefix and trailing are optional. So if the message starts with an @, everything until the first space are the tags. If the following part starts with a colon, then everything until the next space is the prefix. Then everything until either the next colon or the end is the command and parameters, the first word being the command. The trailing is everything after the colon up until the end, which contains the actual chat message. (Also check out the RFC.)

When you parse that you’re left with variables like tags, prefix, command, parameters and trailing. The tags are separated by semicolons (you already split with that) and the key of each tag goes to the first = character. Also be aware of escape characters as specified in the spec (probably not revelant yet in your case, but you should handle them).

Once you parsed the tags as well, you can do stuff like if (command == "USERNOTICE" && tags['msg-id'] == "resub") and resubMessage = trailing. The channel would be parameters[0] (assuming you split them by space).

That’s very useful, thank you. I’ll add that over the next few days, and let you know how it goes!

Thanks again.

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