TwitchLib icon indicating copy to clipboard operation
TwitchLib copied to clipboard

No Example provided to change credentials while Bot is running

Open Snickbrack opened this issue 1 year ago • 9 comments

Hi,

can you please provide some kind of working example on how to set new Credentials for TwitchAPI and the Client while it still runs?

Snickbrack avatar Jul 02 '23 10:07 Snickbrack

Because with my approach:

twitchAPI = new TwitchAPI();
twitchAPI.Settings.ClientId = GetIdentity(fromChannel, toChannel).ClientID;
twitchAPI.Settings.AccessToken = GetIdentity(fromChannel, toChannel).TwitchAuthObject.AccessToken;

ReSendPubSubTopics();

client.Disconnect();
Thread.Sleep(5000);
client.Initialize(credentials, channelInformation.ChannelName);
Thread.Sleep(5000);
client.Connect();

It somehow seems to connect to the same channel multiple times over the it is running. But as you see it is always disconnected and then connected again:

image

The Event OnJoinedChannel gets called over and over again as you see. My Code for the Event is simple:

private void Client_OnJoinedChannel(object sender, OnJoinedChannelArgs e)
{
    if (e.Channel.Equals(channelInformation.ChannelName))
    {
        Logger.Info("Client_OnJoinedChannel");
        TwitchHelper.ClientSendMessage(client, channelInformation.ChannelName, "Achtung, der Sheriff ist wieder in der Stadt! peepoPooPoo ").GetAwaiter().GetResult();
        Logger.Info($"Connected to {e.Channel} with Username {e.BotUsername}");
    }
}

Snickbrack avatar Jul 02 '23 11:07 Snickbrack

And just as a note: Without calling initialize everytime I change the credentials nothing happens at all at connecting.

If I use:

Thread.Sleep(5000);
client.Connect();
client.JoinChannel(channelInformation.ChannelName);

Then it seems to be connected to Twitch and the chat but as I try to send a message as above in Client_OnJoinedChannel it seems to be "holded" somewhere.

Snickbrack avatar Jul 03 '23 10:07 Snickbrack

Okay I would still need an example for this one. But for now as a hotfix I made this one:

ReSendPubSubTopics();

client.Disconnect();
Thread.Sleep(5000);

if (clientInitialized)
{
client = null;
ResetClient();
}

client.Initialize(credentials, channelInformation.ChannelName);
Thread.Sleep(5000);
client.Connect();

clientInitialized = true;

This works for now.

But this is absolutely disgusting and should not be for long in my code.

Snickbrack avatar Jul 03 '23 14:07 Snickbrack

Currently, pubsub is broken. I advise you, at the moment, not to use it. Community members are trying to totally overhaul of code. -> Dev branch.

If I remember correctly, inicialize saves credentials and automaticaly refreshes your access token using oAuth Token... If you provide it. If you need to change credentials, then indeed, you have to initialize client again.

Twitch lib has its fuctions mostly solved asynchonously, but it still needs some real time processing. Thread.Sleep(5000); could cause issues thanks to its blocking behavior. Rather use Task.Delay

Here is example of inicialization before connection

 var capabilities = new Capabilities(true, true, true);
var userCredentials = new ConnectionCredentials(botName, credentials.OAuth, capabilities: capabilities);
var clientOptions = new ClientOptions
                {
                    MessagesAllowedInPeriod = _settings.MessagesAllowedInSingleInterval,
                    ThrottlingPeriod = _settings.MessageThrottlingInterval
                };
var customClient = new WebSocketClient(clientOptions);
_twitchClient = new TwitchClient(customClient, ClientProtocol.WebSocket, _twitchClientLogger);
_twitchClient.AddChatCommandIdentifier(CommandSymbol);
_twitchClient.Initialize(userCredentials, _targetChannel);

Hope this will help.

GimliCZ avatar Jul 06 '23 16:07 GimliCZ

Edited: pubsub won't work with latest release same time as client. See posts below

It won't work with dev unless the package has been updated for the latest communications package. Gets more complicated if your using client with the new communication package in dev as well since it will want the newest comm but won't work for pubsub etc... That being said it is being reworked so expect changes with a new major release on client soon.

For least issues just use release packages for all until the new one is released.

Anyway I agree thread sleep isn't a great idea. Async is better if you know how to set it up.

To change your credentials you would disconnect and reinitialize, then connect again. However ..... /

The problem right now , disconnect call in client is the issue. The work around is to have an object you can dispose and start a new one until the new version comes out or wait.

In short the disconnect tried to reconnect instead of disconnecting causing the spam

Mahsaap avatar Jul 06 '23 18:07 Mahsaap

Wait why was pubsub mentioned in the first place, op only asked about client 🤔 me confused. Either way they all "work" just some issues that can be worked around.

Mahsaap avatar Jul 06 '23 18:07 Mahsaap

Cause at current state master of pubsub bugs out master of client.

As referenced by Bukk #1104 Automatic reconnect after Client/PubSub disconnect

GimliCZ avatar Jul 06 '23 19:07 GimliCZ

And since I saw ReSendPubSubTopics(); I knew he uses pubsub.

GimliCZ avatar Jul 06 '23 19:07 GimliCZ

Most be newer them. Thanks for clarifying. I think that might be because one already uses the new communications and you can't have both. Thanks again.

But I think the main problem here was using client.disconnect. as it had reconnect right in its code. Which will be fixed with the new major update as you mentioned.

Mahsaap avatar Jul 06 '23 19:07 Mahsaap