Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task RunBot()
- {
- botRunning = false;
- try
- {
- #region Connect to Twitch
- /* Twitch Api
- * Initialize Api
- * Collect Channel Informations */
- api = new TwitchAPI();
- await api.InitializeAsync(ClientID, AccessToken);
- var channel = await api.Channels.v5.GetChannelAsync(ChannelID);
- /* Initialize a TwitchClient Connection to the Twitch Chat
- * Creating Login Credentials
- * Creating a Joined Channel */
- ConnectionCredentials credentials = new ConnectionCredentials("Botname", OAuth);
- client = new TwitchClient();
- client.Initialize(credentials, joinedchannel);
- client.Connect();
- /*Twitch Client Events */
- client.OnMessageReceived += OnMessageReceived;
- client.OnLog += OnLog;
- client.OnDisconnected += OnDisconnected;
- client.OnWhisperReceived += OnWhisperReceived;
- client.OnNewSubscriber += OnNewSubscriber;
- client.OnReSubscriber += OnResubscriber;
- client.OnGiftedSubscription += OnGiftedSubscription;
- client.OnUserBanned += OnBannedUser;
- client.OnConnectionError += OnConnectionError;
- client.OnJoinedChannel += OnJoinenChannel;
- /* PubSub Service
- * Dissconect before Starting the PubSub Service to avoid Loops */
- TwitchPubSub pubSub = new TwitchPubSub();
- pubSub.OnPubSubServiceConnected += onPubSubConnected;
- pubSub.OnListenResponse += OnListenResponse;
- pubSub.OnBitsReceived += OnBitsReceived;
- pubSub.Disconnect();
- pubSub.Connect();
- /** Follower Service
- * Disconnect before Starting the Follower Service to avoid Loops*/
- FollowerService followerService = new FollowerService(api, 60, 25);
- followerService.SetChannelByChannelId(channel.Id);
- followerService.OnNewFollowersDetected += onFollowersDetected;
- followerService.OnServiceStarted += OnFollowServiceStarted;
- followerService.StopService();
- await followerService.StartService();
- #endregion
- while (!client.IsConnected) // Wait until Connected
- {
- await Task.Delay(500);
- ApplyLog("Attempting to Connect to Twitch Api...");
- }
- /* Listening to PubSub Events
- * Moved to here because PubSub dont have a bool for IsConnected */
- await Task.Delay(500);
- pubSub.ListenToBitsEvents(channel.Id);
- pubSub.SendTopics(ChannelID);
- botRunning = true;
- }
- catch
- {
- client.Reconnect();
- client.JoinChannel(joinedchannel, true);
- botRunning = false;
- ApplyLog("Can't connect to Stream! Try again in one second...");
- await Task.Delay(1000);
- //await Reconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement