Advertisement
Guest User

Untitled

a guest
May 24th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public async Task RunBot()
  2. {
  3. botRunning = false;
  4. try
  5. {
  6. #region Connect to Twitch
  7. /* Twitch Api
  8. * Initialize Api
  9. * Collect Channel Informations */
  10. api = new TwitchAPI();
  11. await api.InitializeAsync(ClientID, AccessToken);
  12. var channel = await api.Channels.v5.GetChannelAsync(ChannelID);
  13.  
  14. /* Initialize a TwitchClient Connection to the Twitch Chat
  15. * Creating Login Credentials
  16. * Creating a Joined Channel */
  17. ConnectionCredentials credentials = new ConnectionCredentials("Botname", OAuth);
  18. client = new TwitchClient();
  19. client.Initialize(credentials, joinedchannel);
  20. client.Connect();
  21.  
  22. /*Twitch Client Events */
  23. client.OnMessageReceived += OnMessageReceived;
  24. client.OnLog += OnLog;
  25. client.OnDisconnected += OnDisconnected;
  26. client.OnWhisperReceived += OnWhisperReceived;
  27. client.OnNewSubscriber += OnNewSubscriber;
  28. client.OnReSubscriber += OnResubscriber;
  29. client.OnGiftedSubscription += OnGiftedSubscription;
  30. client.OnUserBanned += OnBannedUser;
  31. client.OnConnectionError += OnConnectionError;
  32. client.OnJoinedChannel += OnJoinenChannel;
  33.  
  34. /* PubSub Service
  35. * Dissconect before Starting the PubSub Service to avoid Loops */
  36. TwitchPubSub pubSub = new TwitchPubSub();
  37. pubSub.OnPubSubServiceConnected += onPubSubConnected;
  38. pubSub.OnListenResponse += OnListenResponse;
  39. pubSub.OnBitsReceived += OnBitsReceived;
  40. pubSub.Disconnect();
  41. pubSub.Connect();
  42.  
  43.  
  44. /** Follower Service
  45. * Disconnect before Starting the Follower Service to avoid Loops*/
  46. FollowerService followerService = new FollowerService(api, 60, 25);
  47. followerService.SetChannelByChannelId(channel.Id);
  48. followerService.OnNewFollowersDetected += onFollowersDetected;
  49. followerService.OnServiceStarted += OnFollowServiceStarted;
  50. followerService.StopService();
  51. await followerService.StartService();
  52.  
  53.  
  54. #endregion
  55.  
  56. while (!client.IsConnected) // Wait until Connected
  57. {
  58. await Task.Delay(500);
  59. ApplyLog("Attempting to Connect to Twitch Api...");
  60. }
  61.  
  62. /* Listening to PubSub Events
  63. * Moved to here because PubSub dont have a bool for IsConnected */
  64.  
  65. await Task.Delay(500);
  66. pubSub.ListenToBitsEvents(channel.Id);
  67. pubSub.SendTopics(ChannelID);
  68.  
  69. botRunning = true;
  70.  
  71.  
  72.  
  73. }
  74. catch
  75. {
  76.  
  77. client.Reconnect();
  78. client.JoinChannel(joinedchannel, true);
  79.  
  80. botRunning = false;
  81. ApplyLog("Can't connect to Stream! Try again in one second...");
  82. await Task.Delay(1000);
  83. //await Reconnect();
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement