Advertisement
Guest User

Untitled

a guest
Dec 8th, 2017
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net.Sockets;
  11. using System.IO;
  12. using System.Timers;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15.  
  16.  
  17. namespace Twitch
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         TcpClient tcpclient;
  22.         StreamReader reader;
  23.         StreamWriter writer;
  24.         StreamWriter writerTMI;
  25.         string channel = "nizlmmk";
  26.         Question question = new Question();
  27.         static string[] questions = File.ReadAllLines("questionsDB.txt");
  28.         static Random r = new Random();
  29.         string[] split;
  30.         bool wyrRunning = false;
  31.  
  32.  
  33.         public Form1()
  34.         {
  35.             InitializeComponent();
  36.             Reconnect();
  37.         }
  38.  
  39.         private void Reconnect()
  40.         {
  41.             tcpclient = new TcpClient("irc.chat.twitch.tv", 6667);
  42.             reader = new StreamReader(tcpclient.GetStream());
  43.             writer = new StreamWriter(tcpclient.GetStream());
  44.  
  45.             var userName = "nizlmmk";
  46.             var nickName = "nizlBOT";
  47.             var password = File.ReadAllText("password.txt");
  48.  
  49.             writer.WriteLine("PASS " + password + Environment.NewLine
  50.                 + "NICK " + nickName + Environment.NewLine
  51.                 + "USER " + userName + " 8 * :" + userName);
  52.             writer.WriteLine("CAP REQ :twitch.tv/membership");
  53.             writer.WriteLine("JOIN #nizlmmk");
  54.            
  55.             writer.Flush();
  56.  
  57.         }
  58.  
  59.         void timer1_Tick(object sender, EventArgs e)
  60.         {
  61.            
  62.             if (!tcpclient.Connected)
  63.             {
  64.                 Reconnect();      
  65.             }
  66.            
  67.             if(tcpclient.Available > 0 || reader.Peek() >= 0)
  68.             {
  69.                 var message = reader.ReadLine();
  70.  
  71.                 if  (message.Contains("PRIVMSG"))
  72.             {
  73.                 string fMsg;
  74.                 int removeIndex = message.IndexOf('#') - message.IndexOf('!');
  75.                 fMsg = message.Remove(message.IndexOf('!'), removeIndex);
  76.                 fMsg = fMsg.Remove(fMsg.IndexOf('#'), 8);
  77.                 label1.AppendText(fMsg);
  78.                 if (message.StartsWith("!"))
  79.                 {
  80.                     findCommand(message);
  81.                 }
  82.             }
  83.             else
  84.             {
  85.                 label1.AppendText(message);
  86.             }
  87.             label1.AppendText(Environment.NewLine);
  88.         }
  89.  
  90.         public void displayResults()
  91.         {
  92.                 sendChat("The questions were: ");
  93.                 sendChat($"1) {split[0]}");
  94.                 sendChat("OR");
  95.                 sendChat($"2) {split[1]}");
  96.                 sendChat(Convert.ToString(question.getSelected1()) + " Users selected option 1 ");
  97.                 sendChat(Convert.ToString(question.getSelected2()) + " Users selected option 2 ");
  98.                 question.resetSelected();
  99.  
  100.         }
  101.  
  102.         private void findCommand(string message)
  103.         {
  104.  
  105.             if(message.Contains("!1"))
  106.             {
  107.                 question.incSelected1();
  108.             }
  109.  
  110.             if (message.Contains("!2"))
  111.             {
  112.                 question.incSelected2();
  113.             }
  114.  
  115.             if (message.EndsWith("!wyr"))
  116.             {
  117.                     timer2.Enabled = true;
  118.                     wyrRunning = true;
  119.                     sendQuestion();        
  120.             }
  121.         }
  122.  
  123.         public void sendQuestion()
  124.         {
  125.             string q = questions[r.Next(questions.Length)];
  126.             //sendChat("would you rather?");
  127.             if (Regex.IsMatch(q, @"\sor\s"))
  128.             {
  129.                 split = Regex.Split(q, @"\sor\s");
  130.                 sendChat($"1) {split[0]}");
  131.                 sendChat("OR");
  132.                 sendChat($"2) {split[1]}");
  133.             }
  134.         }
  135.  
  136.         private void sendChat(string toChat)
  137.         {
  138.             writer.WriteLine(":nizlmmk!nizlmmk@nizlmmk.tmi.twitch.tv PRIVMSG #" + channel + " : " + toChat);
  139.             writer.Flush();
  140.         }
  141.  
  142.         private void sendUser(string user, string toChat)
  143.         {
  144.             writer.WriteLine(":" + user + "!" + user + "@" + user + ".tmi.twitch.tv PRIVMSG #" + channel + " : " + toChat);
  145.             writer.Flush();
  146.         }
  147.  
  148.         void timer2_Tick(object sender, EventArgs e)
  149.         {
  150.            
  151.             if(wyrRunning)
  152.             {
  153.                 displayResults();
  154.                 timer2.Enabled = false;
  155.                 wyrRunning = false;
  156.             }
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement