tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.55k stars 214 forks source link

Incorrect number of months on subs and resubs #325

Closed DigitalVortex92 closed 5 years ago

DigitalVortex92 commented 5 years ago

Actual behaviour: "sub for 9 months" shows 0 for the number of months in the resub and sub events

Expected behaviour: It should show 9 months

A fix would be to change the property from msg-param-months to msg-param-cumulative-months, but I am not sure if this would work for all cases as I have yet to run into each case where months is provided.

AlcaDesign commented 5 years ago

The 0 is intentional by Twitch as it's been deprecated. See this comment on the Twitch Discourse.

You should be able to use the userstate to get those parameters before the library is updated.

client.on('resub', (channel, username, _months, message, userstate, methods) => {
    let streakMonths = userstate['msg-param-streak-months']; // number of streak months
    let cumulativeMonths = userstate['msg-param-cumulative-months']; // number of cumulative months subscribed
    let shouldShareStreak = userstate['msg-param-should-share-streak']; // Bool on whether the user has opted to share streak-months
});
client.on('subscription', (channel, username, method, message, userstate) => {
    // I think those tags are available but they're all `0` for new subs.
});

I'm not sure if broken streaks will count as a subscription or resub upon them subscribing again.

AlcaDesign commented 5 years ago

And on that, should the months argument be msg-param-streak-months or msg-param-cumulative-months? And then add another argument on the end .. which is a gross idea but better than changing major versions just to reduce these arguments.

DigitalVortex92 commented 5 years ago

I'd argue months would be cumulative and up to the dev using this to reach streaks, or make months an object which stores both.

On Tue, Feb 5, 2019, 5:45 AM Jacob Foster <notifications@github.com wrote:

And on that, should the months argument be msg-param-streak-months or msg-param-cumulative-months? And then add another argument on the end .. which is a gross idea but better than changing major versions just to reduce these arguments.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tmijs/tmi.js/issues/325#issuecomment-460591735, or mute the thread https://github.com/notifications/unsubscribe-auth/AHsPL-WsPVlscOuF3OcW6fmmsKLSaI9gks5vKWCxgaJpZM4aisPj .

DigitalVortex92 commented 5 years ago

Apologies for the double spam as I'm using email ATM. As far as subs and resubs go, I think it's only a "sub" on first subscription now. At least that's what it seems to be based on what I was working with earlier

On Tue, Feb 5, 2019, 5:52 AM Christopher Curtis < curtis.christopher96@gmail.com wrote:

I'd argue months would be cumulative and up to the dev using this to reach streaks, or make months an object which stores both.

On Tue, Feb 5, 2019, 5:45 AM Jacob Foster <notifications@github.com wrote:

And on that, should the months argument be msg-param-streak-months or msg-param-cumulative-months? And then add another argument on the end .. which is a gross idea but better than changing major versions just to reduce these arguments.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tmijs/tmi.js/issues/325#issuecomment-460591735, or mute the thread https://github.com/notifications/unsubscribe-auth/AHsPL-WsPVlscOuF3OcW6fmmsKLSaI9gks5vKWCxgaJpZM4aisPj .

IPracZ commented 5 years ago

How would a complete code look like, which makes the bot send a response message to a sub or resub. After a resub of a certain monthly length it should send a message back saying '[user] Thanks for your resub of [months] censecutive months'. It can also say the over all months subcribed. When I try to set up such a code, it works about 1 minute and then a error message shows up. I don´t know what I am doing wrong. I instantly deleted the code because I was so frustrated, so I cannot event give examples how I tried it.

AlcaDesign commented 5 years ago

@IPracZ Let me know if this helps you:

const tmi = require('tmi.js');
const client = new tmi.client({
    config: {
        debug: true
    },
    connection: {
        reconnect: true,
        secure: true
    },
    identity: {
        username: 'bot_account',
        password: 'bot_account_token'
    },
    channels: [ 'my_channel' ]
});

client.connect();

client.on('subscription', (channel, username, method, message, userstate) => {
    console.log('subscription', { channel, username, method, message, userstate });
    client.say(channel, `Thanks for subscribing, ${username}!`);
});

client.on('resub', (channel, username, _months, message, userstate, methods) => {
    console.log('resub', { channel, username, message, userstate, methods });
    let streakMonths = userstate['msg-param-streak-months'];
    let cumulativeMonths = userstate['msg-param-cumulative-months'];
    let sharedStreak = userstate['msg-param-should-share-streak'];
    if(sharedStreak) {
        client.say(channel, `Thanks for resubscribing for ${streakMonths} consecutive months, ${username}!`);
    }
    else {
        client.say(channel, `Thanks for resubscribing for ${cumulativeMonths} months, ${username}!`);
    }
});
IPracZ commented 5 years ago

@AlcaDesign works perfectly now, didn´t know that I can console.log the userstate ... And what does the sharedStreak include in it ? At some point this error message pops up:

TypeError: Cannot read property 'msg-param-streak-months' of null at client.on (D:\TCB2\app.js:33:30) at client.EventEmitter.emit (D:\TCB2\node_modules\tmi.js\lib\events.js:101:25) at client.EventEmitter.emits (D:\TCB2\node_modules\tmi.js\lib\events.js:64:19) at client.handleMessage (D:\TCB2\node_modules\tmi.js\lib\client.js:581:30) at parts.forEach (D:\TCB2\node_modules\tmi.js\lib\client.js:962:36) at Array.forEach () at client._onMessage (D:\TCB2\node_modules\tmi.js\lib\client.js:961:11) at WebSocket.onMessage (D:\TCB2\node_modules\ws\lib\event-target.js:120:16) at WebSocket.emit (events.js:189:13) at Receiver.receiverOnMessage (D:\TCB2\node_modules\ws\lib\websocket.js:742:20)

btw this happenend after a Twitch Prime Sub, with a certain amount of over all months and no message, don´t know if that helps

And in addition to that, i feel like that the scipt detects the sub earlier than it shows up in chat, will the response message still be sent after the sub poped up in chat ?

IPracZ commented 5 years ago

And do I have to include this Ping - Pong response, because I read something that twitch sends pings and when there is no response of the bot, it will be disconnected. Do I need something like that?

AlcaDesign commented 5 years ago

@IPracZ tmi.js automatically handles the pong replies.

https://github.com/tmijs/tmi.js/blob/c6114871aea35b98bff67ef05ab15ee8c9a8e888/lib/client.js#L108-L112

I wrote the line "console.log('resub', { channel, username, message, userstate, methods });" in my example. Did you see that get logged to the console before that error?

IPracZ commented 5 years ago

yeah I see it being logged, it does work for some resubs aswell, but for some other resubs it doesn´t and I cannot really see a difference between those specific resubs

IPracZ commented 5 years ago

And in addition the resubs are being detected before the message pops up in chat, which means that the bot sends his message before the actual resub message appears in chat

AlcaDesign commented 5 years ago

@IPracZ Could you share what's being logged just before the error occurs? Or are you saying that the error is happening without that console.log occurring? Are you sure you're using tmi.js@1.3.0 and not the current GitHub repo?

Do be wary of writing multiple messages to this thread, this isn't exactly a chat. ;) You can join the unofficial "Twitch API" Discord server (https://discord.gg/8NXaEyV) where I and many other third-party devs have channels for our libraries. Or you could join the official TwitchDev Discord (https://link.twitch.tv/devchat).

ak505188 commented 5 years ago

I don't have the biggest sample size, but it seems like userstate is null when msg is null. If the subscription has a message, userstate is set properly. Otherwise, both msg and userstate are null.

AlcaDesign commented 5 years ago

@ak505188 This has been addressed for the upcoming v1.4. See https://github.com/tmijs/tmi.js/commit/6b1c25e00e311bbe0f2c132b47c82f34215e7810#diff-50cfa59973c04321b5da0c6da0fdf4feR619

Kelin2025 commented 5 years ago

Hey @AlcaDesign Any approximate date for 1.4 release? This bug breaks a significant part of my app 🤔