TwitchLib / TwitchLib.Unity

TwitchLib repository representing all code belonging to the implementation of TwitchLib for Unity. Maintained primarily by LuckyNoS7evin.
164 stars 33 forks source link

PubSub not registering any events incoming from Twitch #62

Open NanoSNAkeR opened 3 years ago

NanoSNAkeR commented 3 years ago

I Started having problems with the PubSub service all of a sudden, It's not getting any input from twitch events. The strange thing is it used to work without a problem right before I started a stream, now it just says that everything connects successfully but it doesn't register any events incoming from Twitch, like channel points redeems, whispers or follows... I don't know what else to do...

Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TwitchLib.Unity;
using TwitchLib.PubSub;
using TwitchLib.PubSub.Events;

public class TwitchPubSubTest : MonoBehaviour
{
    private PubSub _pubSub;

    //public string currentReward = "";
    //public string latestFollower = "";

    // Start is called before the first frame update
    void Start()
    {
        Application.runInBackground = true;

        _pubSub = new PubSub();

        _pubSub.OnChannelPointsRewardRedeemed += OnChannelPointsRewardRedeemed;
        //_pubSub.OnFollow += _pubSub_OnFollow;
        _pubSub.OnWhisper += OnWhisper;
        _pubSub.OnListenResponse += OnListenResponse;
        _pubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;

        _pubSub.Connect();
    }

    private void OnPubSubServiceConnected(object sender, System.EventArgs e)
    {
        Debug.Log("PubSubServiceConnected!");

        // CHANNEL_ID is the streamer channel ID the one that is like 12345678.
        // CHANNEL_ACCESS_TOKEN is the channel oauth token, which is validated for everything.

        _pubSub.ListenToWhispers(Secrets.CHANNEL_ID);
        _pubSub.ListenToChannelPoints(Secrets.CHANNEL_ID);
        //_pubSub.ListenToFollows(Secrets.CHANNEL_ID);
        _pubSub.SendTopics(Secrets.CHANNEL_ACCESS_TOKEN);
    }

    private void OnListenResponse(object sender, OnListenResponseArgs e)
    {
        if (e.Successful)
        {
            Debug.Log($"Successfully verified listening to topic: {e.Topic}");
        }
        else
        {
            Debug.Log($"Failed to listen! Error: {e.Response.Error}");
        }
    }

    //private void _pubSub_OnFollow(object sender, OnFollowArgs e)
    //{
    //    Debug.Log($"{e.Username}" + " Has followed!");
    //    latestFollower = $"{e.Username}";
    //    FollowThanks.instance.StartAction(latestFollower);
    //}

    private void OnChannelPointsRewardRedeemed(object sender, OnChannelPointsRewardRedeemedArgs e)
    {
        Debug.Log("A channel reward has been redeemed! " + $"{e.RewardRedeemed.Redemption.Reward.Title}");
        //currentReward = $"{e.RewardRedeemed.Redemption.Reward.Title}";
    }

    private void OnWhisper(object sender, OnWhisperArgs e)
    {
        Debug.Log($"{e.Whisper.Data}");
    }
}

And here's the console log for the script... as you can see it verifies everything successfully...

response

Also the other services, such as the Client and the API work correctly without any errors, bot reads chat, reads viewers, can answer to callouts etc...

If anyone can help I would appreciate it a lot!

MelonSpeedruns commented 3 years ago

I can confirm I'm having the exact same issue. My code is identical to yours.

ZePilOt commented 3 years ago

I have the same issue, reverted back to ListenToRewards. It's strangely linked to rewards with images. If they don't have images, it's working (but you need to restart your app first : first rewards with images will break the event entirely)

NanoSNAkeR commented 3 years ago

I have the same issue, reverted back to ListenToRewards. It's strangely linked to rewards with images. If they don't have images, it's working (but you need to restart your app first : first rewards with images will break the event entirely)

Yeah, I also traced the problem to the new channel points function but I didn't know the images were the actual problem... most curious. Switched to the ListenToRewards as well and it seems to work like a charm. Also found out that the Whisper function can also cause some problems too...

stickermonster commented 3 years ago

Just wanted to chime in--had the same issue, switching back to the old OnRewardRedeemed system worked just fine :)

I'm wondering why the custom image messes the redeem up--it must have something to do with how the message is formatted; the reward listener is looking for a specific first arg, but that arg must change to be the image when one is present.

gengar1603 commented 2 years ago

I can confirm that OnChannelPointsRewardRedeemed still has problems with custom reward icons.

LazyFangs commented 2 years ago

To give closure to this issue (and because I invested nearly 6 hours going on the same journey as the rest of the guys here)

  1. The bug was using an older version of PubSub, which had incorrectly set the Image object as String instead of an object - hence the String parser threw up an "unexpected character {". Curly braces are not how you start a string. It was fixed at a later date than the current Release https://github.com/TwitchLib/TwitchLib.PubSub/commit/e65f966e47c3f9a171b8cf4f58a8b92cab2f839a#diff-dd87ee3df01f86fd8eb906d454236162c2742a7f7e0d0d81623fc910c1153660

  2. The current pre-release version has this fix already applied.

Sorry for the necro, but this problem drove me up a wall and I thought it only fair to have at least one more issue semi-closed

MelonSpeedruns commented 2 years ago

I will try that and report today if the pre-release version indeed fixed it on my end. Thanks for letting me know!

EDIT: Hmm, it doesn't like it's working with PubSub version 3.2.3, which has the fix. I tried using the following code in a basic Console App using Visual Studio 2022:

using TwitchLib.PubSub;
using TwitchLib.PubSub.Events;

TwitchPubSub _pubSub = new TwitchPubSub();

_pubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;
_pubSub.OnChannelPointsRewardRedeemed += OnChannelPointsRewardRedeemed;

_pubSub.ListenToChannelPoints("63469890");

_pubSub.Connect();

void OnChannelPointsRewardRedeemed(object? sender, OnChannelPointsRewardRedeemedArgs e)
{
    Console.WriteLine(e.RewardRedeemed.Redemption.Reward.Title);
}

void OnPubSubServiceConnected(object? sender, EventArgs e)
{
    _pubSub.SendTopics();
    Console.WriteLine("Connected to Twitch! Press Escape to exit.");
}

ConsoleKeyInfo input;
do
{
    input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);

It works perfectly fine with the deprecated functions however.

MeepsKitten commented 2 years ago

I will try that and report today if the pre-release version indeed fixed it on my end. Thanks for letting me know!

EDIT: Hmm, it doesn't like it's working with PubSub version 3.2.3, which has the fix. I tried using the following code in a basic Console App using Visual Studio 2022:

using TwitchLib.PubSub;
using TwitchLib.PubSub.Events;

TwitchPubSub _pubSub = new TwitchPubSub();

_pubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;
_pubSub.OnChannelPointsRewardRedeemed += OnChannelPointsRewardRedeemed;

_pubSub.ListenToChannelPoints("63469890");

_pubSub.Connect();

void OnChannelPointsRewardRedeemed(object? sender, OnChannelPointsRewardRedeemedArgs e)
{
    Console.WriteLine(e.RewardRedeemed.Redemption.Reward.Title);
}

void OnPubSubServiceConnected(object? sender, EventArgs e)
{
    _pubSub.SendTopics();
    Console.WriteLine("Connected to Twitch! Press Escape to exit.");
}

ConsoleKeyInfo input;
do
{
    input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);

It works perfectly fine with the deprecated functions however.

It does seem to work fine, I think you were just missing OAuth in SendTopics

midnitefactory commented 1 year ago

The issue persists even with the most up to date version of this library despite what's said in the comments. Even with OAuth properly authorized and sent via SendTopics it's the OnChannelPointsRewardRedeemed function that causes PubSub to crash, using OnRewardRedeemed fixed everything.

HugoVG commented 12 months ago

The issue persists even with the most up to date version of this library despite what's said in the comments. Even with OAuth properly authorized and sent via SendTopics it's the OnChannelPointsRewardRedeemed function that causes PubSub to crash, using OnRewardRedeemed fixed everything.

i'm facing the same issue of it not firing, are you still using client.ListenToRewards(id); or are you using client.ListenToChannelPoints(id);?

midnitefactory commented 12 months ago

The issue persists even with the most up to date version of this library despite what's said in the comments. Even with OAuth properly authorized and sent via SendTopics it's the OnChannelPointsRewardRedeemed function that causes PubSub to crash, using OnRewardRedeemed fixed everything.

i'm facing the same issue of it not firing, are you still using client.ListenToRewards(id); or are you using client.ListenToChannelPoints(id);?

ListenToRewards(id) is what I use and it works