//start listening for donations, open status window
        public void LaunchWindow(object sender, RoutedEventArgs e)
        {
            if (!Directory.Exists("output"))
            {
                DirectoryInfo di = Directory.CreateDirectory("output");
            }
            string         url = "https://www.twitchalerts.com/api/v1.0/user?access_token=" + AccessToken.Password;
            HttpWebRequest request;

            request        = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            window2.Show();
            string         url2 = "https://api.twitch.tv/kraken/channel";
            HttpWebRequest request2;

            request2        = (HttpWebRequest)WebRequest.Create(url2);
            request2.Method = "GET";
            request2.Headers["Client-ID"]     = "9tztppkgkayamw5135n4uo216fwvoy";
            request2.Headers["Authorization"] = "OAuth " + TwitchAccessToken.Password;
            try
            {
                WebResponse  resp               = request.GetResponse();
                Stream       dataStream         = resp.GetResponseStream();
                StreamReader reader             = new StreamReader(dataStream);
                string       responseFromServer = reader.ReadToEnd();
                dataStream.Close();
                string json = responseFromServer;
                User   user = JsonConvert.DeserializeObject <User>(json);
                my_username = user.GetTwitch().getName();
                my_user_id  = user.GetTwitch().GetId();
                WebResponse  resp2               = request2.GetResponse();
                Stream       dataStream2         = resp2.GetResponseStream();
                StreamReader reader2             = new StreamReader(dataStream2);
                string       responseFromServer2 = reader2.ReadToEnd();
                dataStream2.Close();
                string     json2      = responseFromServer2;
                TwitchUser twitchuser = JsonConvert.DeserializeObject <TwitchUser>(json2);

                //set up timer for streamlabs donation checker
                void updateWindow(object source, ElapsedEventArgs ev)
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        UpdateDonations(user.GetTwitch().GetId());
                    });
                }
                UpdateDonations(user.GetTwitch().GetId());
                System.Timers.Timer myTimer = new System.Timers.Timer();
                myTimer.Elapsed += new ElapsedEventHandler(updateWindow);
                myTimer.Interval = 10000;
                myTimer.Enabled  = true;
                List <string> bidwarOptions = new List <string>();
                foreach (object child in TextBoxes.Children)
                {
                    var textbox = (TextBox)child;
                    if (textbox.Text != "")
                    {
                        bidwarOptions.Add(textbox.Text);
                    }
                }

                //set up IRC listener for subs, resubs, and corrections
                void doIRC()
                {
                    IRCConfig conf = new IRCConfig();

                    conf.name     = my_username;
                    conf.nick     = my_username;
                    conf.port     = 6667;
                    conf.channel  = "#" + my_username;
                    conf.server   = "irc.chat.twitch.tv";
                    conf.password = "******" + TwitchAccessToken.Password;
                    conf.joined   = false;


                    IRCBot bot = new IRCBot(conf);

                    using (bot)
                    {
                        System.Diagnostics.Debug.WriteLine("connecting");
                        bot.Connect();
                        bot.IRCWork(user.GetTwitch().GetId(), bidwarOptions);
                    }
                }
                Thread irc = new Thread(new ThreadStart(doIRC));
                irc.Start();

                //websocket listener for bits

                /*WebSocket websocket = new WebSocket("wss://pubsub-edge.twitch.tv");
                 * websocket.Opened += new EventHandler(websocket_Opened);
                 * websocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(websocket_Error);
                 * websocket.Closed += new EventHandler(websocket_Closed);
                 * websocket.MessageReceived += new EventHandler<WebSocket4Net.MessageReceivedEventArgs>(websocket_MessageReceived);
                 * websocket.Open();
                 * openListener();
                 * JavaScriptSerializer serializer = new JavaScriptSerializer();
                 * var listen = false;
                 * System.Timers.Timer pingTimer = new System.Timers.Timer();
                 * pingTimer.Elapsed += new ElapsedEventHandler(sendPing);
                 * pingTimer.Interval = 240000;
                 * pingTimer.Enabled = true;
                 * void websocket_Opened(object sender2, EventArgs e2)
                 * {
                 *  Application.Current.Dispatcher.Invoke((Action)delegate {
                 *      var tblock2 = new TextBlock();
                 *      tblock2.Text = "Established connection to bits service";
                 *      tblock2.TextWrapping = System.Windows.TextWrapping.Wrap;
                 *      window2.Logs.Children.Add(tblock2);
                 *  });
                 * }
                 * void websocket_Error(object sender2, SuperSocket.ClientEngine.ErrorEventArgs e2)
                 * {
                 *  Application.Current.Dispatcher.Invoke((Action)delegate {
                 *      var tblock3 = new TextBlock();
                 *      tblock3.Text = "Bits service error: " + e2.Exception.Message;
                 *      tblock3.TextWrapping = System.Windows.TextWrapping.Wrap;
                 *      window2.Logs.Children.Add(tblock3);
                 *  });
                 * }
                 * void websocket_Closed(object sender2, EventArgs e2)
                 * {
                 *  Application.Current.Dispatcher.Invoke((Action)delegate {
                 *      var tblock4 = new TextBlock();
                 *      tblock4.Text = "Closed bits service";
                 *      tblock4.TextWrapping = System.Windows.TextWrapping.Wrap;
                 *      window2.Logs.Children.Add(tblock4);
                 *  });
                 * }
                 * void websocket_MessageReceived(object sender2, MessageReceivedEventArgs e2)
                 * {
                 *  SocketMessage baseMessage = JsonConvert.DeserializeObject<SocketMessage>(e2.Message);
                 *  var output = "";
                 *  if (baseMessage.getType() == "PONG")
                 *  {
                 *      output = "pong";
                 *  }
                 *  else if (baseMessage.getType() == "RECONNECT")
                 *  {
                 *      websocket.Close();
                 *      websocket.Open();
                 *      output = "Reconnecting due to server restart";
                 *  }
                 *  if (baseMessage.getType() == "MESSAGE")
                 *  {
                 *      var container = JsonConvert.DeserializeObject<CheerMessageContainer>(e2.Message);
                 *      CheerMessageData data = container.getData();
                 *      try
                 *      {
                 *          if (data.getTopic() == "channel-bits-events-v1." + twitchuser.getId().ToString())
                 *          {
                 *              CheerContainer thisCheer = JsonConvert.DeserializeObject<CheerContainer>(data.getMessage());
                 *              saveCheer(thisCheer, user.GetTwitch().GetId());
                 *          }
                 *      }
                 *      catch(Exception eee)
                 *      {
                 *          output = e2.Message;
                 *      }
                 *  }
                 *  else
                 *  {
                 *      output = e2.Message;
                 *  }
                 *  Application.Current.Dispatcher.Invoke((Action)delegate {
                 *      var tblock5 = new TextBlock();
                 *      tblock5.Text = "Message from bits service: " + e2.Message;
                 *      tblock5.TextWrapping = System.Windows.TextWrapping.Wrap;
                 *      window2.Logs.Children.Add(tblock5);
                 *  });
                 * }
                 * void sendPing(object source, ElapsedEventArgs ev)
                 * {
                 *  var ping = new { type = "PING" };
                 *  websocket.Send(serializer.Serialize(ping));
                 * }
                 * async void openListener()
                 * {
                 *  await Task.Delay(TimeSpan.FromSeconds(7));
                 *  var ts = new string[1];
                 *  ts[0] = "channel-bits-events-v1." + twitchuser.getId().ToString();
                 *  var listener = new { type = "LISTEN", data = new { topics = ts, auth_token = TwitchAccessToken.Password } };
                 *  websocket.Send(serializer.Serialize(listener));
                 * }*/
            }
            catch (Exception ee)
            {
                System.Diagnostics.Debug.WriteLine(ee.Message);
                MessageBox.Show("Could not connect to StreamLabs. Try re-entering your access token. The server may also be down.");
            }
        }
Exemple #2
0
 public IRCBot(IRCConfig config)
 {
     this.config = config;
 }