Advertisement
Guest User

Form1.cs

a guest
Jan 9th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.27 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.IO;
  14. using System.Threading;
  15. using System.Text.RegularExpressions;
  16. using System.Runtime.InteropServices;
  17. using System.Media;
  18. using System.Diagnostics;
  19.  
  20. namespace WindowsFormsApplication1
  21. {
  22.  
  23.     public partial class Form1 : Form
  24.     {
  25.  
  26.         #region variables
  27.         public static string userName = "zexobot";
  28.         public static string password = "oauth:DansGame";
  29.  
  30.         private static IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
  31.         NetworkStream serverStream = default(NetworkStream);
  32.         Thread chatThread;
  33.         IniFile Pointsini = new IniFile(@"C:\Users\Tanner_\Documents\Visual Studio 2015\Projects\ExoBot\ExoBot\Points.ini");
  34.         string readData = "";
  35.         string scrambledWord = "";
  36.         string word = "";
  37.         string url = "https://decapi.me/twitch/uptime?channel=zexodiousz";
  38.         string result = null;
  39.         string viewercount = "https://tmi.twitch.tv/group/user/zexodiousz/chatters";
  40.         Thread oThread = new Thread(new ThreadStart(Broadcast));
  41.         #endregion
  42.  
  43.         #region form open, close, stuff
  44.         public Form1()
  45.         {
  46.             InitializeComponent();
  47.  
  48.         }
  49.  
  50.         private void Form1_Load(object sender, EventArgs e)
  51.         {
  52.             /*while (irc == null)
  53.             {
  54.                 try
  55.                 {
  56.                     IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);*/
  57.                     irc.joinRoom("zexodiousz");
  58.                     chatThread = new Thread(getMessage);
  59.                     chatThread.Start();
  60.                 /*}
  61.                 catch (Exception x)
  62.                 {
  63.                     var message = "Could not connect to the irc channel. Reconnect?";
  64.                     var title = "Error";
  65.                     var result = MessageBox.Show(message, title, MessageBoxButtons.YesNo);
  66.                     switch (result)
  67.                     {
  68.                         case DialogResult.Yes:
  69.                             MessageBox.Show("Reconnecting");
  70.                             break;
  71.                         case DialogResult.No:
  72.                             Application.Exit();
  73.                             break;
  74.                     }
  75.                 }
  76.             }*/
  77.         }
  78.  
  79.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  80.         {
  81.             irc.leaveRoom();
  82.             serverStream.Dispose();
  83.             Environment.Exit(0);
  84.         }
  85.  
  86.         private void getMessage()
  87.         {
  88.             serverStream = irc.tcpClient.GetStream();
  89.             int buffsize = 0;
  90.             byte[] inStream = new byte[10025];
  91.             buffsize = irc.tcpClient.ReceiveBufferSize;
  92.             while (true)
  93.             {
  94.                 try
  95.                 {
  96.                     readData = irc.readMessage();
  97.                     msg();
  98.                 }
  99.                 catch (Exception)
  100.                 {
  101.  
  102.                 }
  103.             }
  104.         }
  105.  
  106.         private void msg()
  107.         {
  108.             if (this.InvokeRequired) this.Invoke(new MethodInvoker(msg));
  109.             else
  110.             {
  111.                 string[] separator = new string[] { "#zexodiousz :" };
  112.                 string[] singlesep = new string[] { ":", "!" };
  113.  
  114.                 if (readData.Contains("PRIVMSG"))
  115.                 {
  116.                     string username = readData.Split(singlesep, StringSplitOptions.None)[1];
  117.                     string message = readData.Split(separator, StringSplitOptions.None)[1];
  118.  
  119.                     if (message[0] == '!') commands(username, message);
  120.  
  121.                     ChatBox.Text = ChatBox.Text + username + "> " + message + Environment.NewLine;
  122.  
  123.                     int num = ChatBox.Lines.Count();
  124.                     if (ChatBox.Lines.Count() > 301)
  125.                     {
  126.                         var foos = new List<string>(ChatBox.Lines);
  127.                         foos.RemoveAt(0);
  128.                         ChatBox.Lines = foos.ToArray();
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.  
  134.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  135.         {
  136.             ChatBox.SelectionStart = ChatBox.Text.Length;
  137.             ChatBox.ScrollToCaret();
  138.         }
  139.  
  140.         private void richTextBox1_TextChanged_1(object sender, EventArgs e)
  141.         {
  142.  
  143.         }
  144.  
  145.         private void label2_Click(object sender, EventArgs e)
  146.         {
  147.  
  148.         }
  149.  
  150.         private void OriginalWord_TextChanged(object sender, EventArgs e)
  151.         {
  152.  
  153.         }
  154.  
  155.         private void ScrambledText_TextChanged(object sender, EventArgs e)
  156.         {
  157.  
  158.         }
  159.  
  160.         private void button1_Click(object sender, EventArgs e)
  161.         {
  162.             irc.sendChatMessage(BotChatBox.Text);
  163.             BotChatBox.Clear();
  164.  
  165.         }
  166.  
  167.         private void BotChatBox_KeyPress(object sender, KeyPressEventArgs e)
  168.         {
  169.             if (e.KeyChar == Convert.ToChar(Keys.Return))
  170.             {
  171.                 irc.sendChatMessage(BotChatBox.Text);
  172.                 BotChatBox.Clear();
  173.             }
  174.         }
  175.  
  176.         private void broadcastContact_Click(object sender, EventArgs e)
  177.         {
  178.             irc.sendChatMessage("You can contact me through snapchat: will_miles11, Twitter: @zexodious, or even on youtube: https://youtube.com/zExodious");
  179.         }
  180.  
  181.         private void ABContact_CheckedChanged(object sender, EventArgs e)
  182.         {
  183.             if (checked(true) && !oThread.IsAlive)
  184.             {
  185.                 oThread.Start();
  186.             }
  187.             else if (unchecked(true) && oThread.IsAlive)
  188.             {
  189.                 oThread.Abort();
  190.                 oThread.Join();
  191.                 oThread = new Thread(new ThreadStart(Broadcast));
  192.             }
  193.         }
  194.  
  195.         private void shuffleButton_Click(object sender, EventArgs e)
  196.         {
  197.             Scramble();
  198.             irc.sendChatMessage("Unscramble this word to win 10 points. usage: !guess [word] > " + scrambledWord + " <");
  199.             OriginalWord.Text = word;
  200.             ScrambledText.Text = scrambledWord;
  201.         }
  202.  
  203.         private void ABShuffle_CheckedChanged(object sender, EventArgs e)
  204.         {
  205.             //THIS IS ONLY IF THIS STOPS WORKING MessageBox.Show("This Is Not Yet Available. Please Use !Shuffle or Click The Shuffle Button");
  206.                 //irc.sendChatMessage("Unscramble this word to win 10 points. usage: !guess [word] > " + scrambledWord + " <");
  207.                 //Thread.Sleep(300000);
  208.         }
  209.  
  210.         private static void Broadcast()
  211.         {
  212.             while (true)
  213.             {
  214.                 irc.sendChatMessage("You can contact me through snapchat: will_miles11, Twitter: @zexodious, or even on youtube: https://youtube.com/zExodious");
  215.                 Thread.Sleep(300000);
  216.             }
  217.  
  218.         }
  219.  
  220.         private void AddPoints(string username, double points)
  221.         {
  222.             double finalnumber = 0;
  223.             try
  224.             {
  225.                 string[] separator = new string[] { @"\r\n" };
  226.                 username = username.Trim().ToLower();
  227.                 string pointsofuser = Pointsini.IniReadValue("#zexodiousz." + username, "Points");
  228.                 double numberofpoints = double.Parse(pointsofuser);
  229.                 finalnumber = Convert.ToDouble(numberofpoints + points);
  230.                 if (finalnumber > 0) Pointsini.IniWriteValue("#zexodiousz." + username, "Points", finalnumber.ToString());
  231.                 if (finalnumber <= 0) Pointsini.IniWriteValue("#zexodiousz." + username, "Points", "0");
  232.             }
  233.             catch (Exception)
  234.             {
  235.                 if (points > 0) Pointsini.IniWriteValue("#zexodiousz." + username, "Points", points.ToString());
  236.  
  237.             }
  238.         }
  239.  
  240.         public void Scramble()
  241.         {
  242.             string[] words = { "home", "food", "game", "rest", "jazz", "joke", "maze", "mojo", "lazy", "zinc", "fuze", "fuse", "juke", "jack", "buzz", "lick", "sick", "path", "snap", "pond", "poor", "rich", "rick", "plan", "team", "cast", "help", "when", "view", "edit", "more", "file", "lore", "sore", "boar", "core", "door", "same", "came", "tame", "lame", "lane", "lack", "data", "lamp", "land", "last", "form", "math", "mast", "code", "toad", "node", "list", "cyst", "wish", "kiss", "miss", "lisp", "fish", "snip", "snap", "crack", "stop", "crop", "drop", "blop", "slop", "mope", "soap", "cope", "coke", "nope", "loop", "soup", "poop", "crap", "crab", "whip", "cream", "shop", "wimp", "limp", "pimp", "wisk", "pint", "tisk", "mite", "dust", "bugs", "hype", "wipe", "tool", "cart", "lite", "shot", "ship", "shin", "chan", "spam", "cram", "spun", "spat", "spit", "unit", "over", "wolf", "fall", "flag", "corn", "worn", "card", "port", "died", "memo", "hole", "coal", "toll", "roll" };
  243.  
  244.             int WORD_SIZE = 4;
  245.  
  246.             // Define some 4-letter words to be scrambled.
  247.             // Define two arrays equal to the number of letters in each word.
  248.             double[] keys = new double[WORD_SIZE];
  249.             string[] letters = new string[WORD_SIZE];
  250.             // Initialize the random number generator.
  251.             Random rnd = new Random();
  252.             //119
  253.             word = words[rnd.Next(119)];
  254.  
  255.             for (int ctr = 0; ctr < word.Length; ctr++)
  256.             {
  257.                 // Populate the array of keys with random numbers.
  258.                 keys[ctr] = rnd.NextDouble();
  259.                 // Assign a letter to the array of letters.
  260.                 letters[ctr] = word[ctr].ToString();
  261.             }
  262.             // Sort the array.
  263.             Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);
  264.             // Display the scrambled word.
  265.             scrambledWord = String.Concat(letters[0], letters[1],
  266.                                                      letters[2], letters[3]);
  267.  
  268.         }
  269.  
  270.         private void button2_Click(object sender, EventArgs e)
  271.         {
  272.             ChatBox.Clear();
  273.         }
  274.  
  275.         private void richTextBox1_TextChanged_2(object sender, EventArgs e)
  276.         {
  277.             return;
  278.         }
  279.  
  280.         private void button2_Click_1(object sender, EventArgs e)
  281.         {
  282.             string modnames = "";
  283.             string viewernames = "";
  284.             Viewercount(ref modnames, ref viewernames, false);
  285.             richTextBox1.Text = "Moderators :" + modnames + "\n\n" + "Viewers :" + viewernames;
  286.         }
  287.  
  288.         private void Viewercount(ref string modnames, ref string viewernames, bool sn = true)
  289.         {
  290.             WebClient viewerclient = new WebClient();
  291.             result = viewerclient.DownloadString(viewercount);
  292.  
  293.  
  294.  
  295.             System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("\"moderators\": \\[[^\\]]*\\]", RegexOptions.Singleline);
  296.  
  297.             if (re.IsMatch(result))
  298.             {
  299.                 string raw = re.Match(result).Value;
  300.                 string rawnames = raw.Split('[')[1];
  301.                 string[] names = rawnames.Split(',');
  302.                 string namestring = "";
  303.                 foreach (string name in names)
  304.                 {
  305.                     namestring += (name + ", ");
  306.                     if (sn) namestring = namestring.Replace("\n", "");
  307.                     namestring = namestring.Replace("\"", "");
  308.                 }
  309.                 namestring = namestring.Split(']')[0];
  310.                 if (string.IsNullOrWhiteSpace(namestring)) namestring = "no moderators present";
  311.                 modnames = namestring;
  312.             }
  313.             re = new System.Text.RegularExpressions.Regex("\"viewers\": \\[[^\\]]*\\]", RegexOptions.Singleline);
  314.             if (re.IsMatch(result))
  315.             {
  316.                 string raw = re.Match(result).Value;
  317.                 string rawnames = raw.Split('[')[1];
  318.                 string[] names = rawnames.Split(',');
  319.                 string namestring = "";
  320.                 foreach (string name in names)
  321.                 {
  322.                     namestring += (name + ", ");
  323.                     if (sn) namestring = namestring.Replace("\n", "");
  324.                     namestring = namestring.Replace("\"", "");
  325.                 }
  326.                 namestring = namestring.Split(']')[0];
  327.                 if (string.IsNullOrWhiteSpace(namestring)) namestring = "no viewers present";
  328.                 viewernames = namestring;
  329.             }
  330.         }
  331.  
  332.  
  333.         #endregion
  334.  
  335.         private void commands(string username, string message)
  336.         {
  337.             string command = message.Split(new[] { ' ', '!' }, StringSplitOptions.None)[1];
  338.  
  339.  
  340.             switch (command.ToLower())
  341.             {
  342.                 case "help":
  343.                     irc.sendChatMessage("@" + username + " Commands: !sr, !points, !uptime, !points, !guess, !fire, !rules, !contact");
  344.                     break;
  345.                 case "reward":
  346.                     if (username == "zexodiousz" || username == "swazzleman")
  347.                     {
  348.                         string recipient = message.Split(new string[] { " " }, StringSplitOptions.None)[1];
  349.                         //if (recipient == username) break;
  350.                         if (recipient[0] == '@')
  351.                         {
  352.                             recipient = recipient.Split(new[] { '@' }, StringSplitOptions.None)[1];
  353.                         }
  354.                         string pointstotransferstring = message.Split(new string[] { " " }, StringSplitOptions.None)[2];
  355.                         double pointstotransfer = 0;
  356.                         bool validnumber = double.TryParse(pointstotransferstring.Split(new[] { ' ' }, StringSplitOptions.None)[0], out pointstotransfer);
  357.                         if (!validnumber) break;
  358.                         if (pointstotransfer > 0)
  359.                         {
  360.                             AddPoints(recipient, pointstotransfer);
  361.                             irc.sendChatMessage("Points sent!");
  362.                         }
  363.                     }
  364.                     break;
  365.                 case "unreward":
  366.                     if (username == "zexodiousz" || username == "swazzleman")
  367.                     {
  368.                         string recipient = message.Split(new string[] { " " }, StringSplitOptions.None)[1];
  369.                         //if (recipient == username) break;
  370.                         if (recipient[0] == '@')
  371.                         {
  372.                             recipient = recipient.Split(new[] { '@' }, StringSplitOptions.None)[1];
  373.                         }
  374.                         string pointstotransferstring = message.Split(new string[] { " " }, StringSplitOptions.None)[2];
  375.                         double pointstotransfer = 0;
  376.                         bool validnumber = double.TryParse(pointstotransferstring.Split(new[] { ' ' }, StringSplitOptions.None)[0], out pointstotransfer);
  377.                         if (!validnumber) break;
  378.                         if (pointstotransfer > 0)
  379.                         {
  380.                             AddPoints(recipient, -pointstotransfer);
  381.                             irc.sendChatMessage("Points removed!");
  382.                         }
  383.  
  384.                     }
  385.                     break;
  386.                 case "points":
  387.                     string yourpoints = Pointsini.IniReadValue("#zexodiousz." + username, "Points");
  388.                     if (yourpoints == "")
  389.                     {
  390.                         irc.sendChatMessage("Your points are 0, @" + username);
  391.  
  392.                     }
  393.                     else
  394.                     {
  395.                         irc.sendChatMessage("Your points are " + yourpoints + ", @" + username);
  396.                     }
  397.                     break;
  398.                 case "shuffle":
  399.                     if (username == "swazzleman" || username == "zexodiousz")
  400.                     {
  401.                         Scramble();
  402.                         irc.sendChatMessage("Unscramble this word to win 10 points. usage: !guess [word] > " + scrambledWord + " <");
  403.                         OriginalWord.Text = word;
  404.                         ScrambledText.Text = scrambledWord;
  405.                     }
  406.                     break;
  407.                 case "guess":
  408.                     string guess = message.Split(new string[] { " " }, StringSplitOptions.None)[1];
  409.                     if (guess == word)
  410.                     {
  411.                         irc.sendChatMessage("Congratulations! You get 10 points!");
  412.                         AddPoints(username, 10);
  413.                     }
  414.                     else
  415.                     {
  416.                         irc.sendChatMessage("Too bad, try again!");
  417.                     }
  418.                     break;
  419.                 case "fire":
  420.                     irc.sendChatMessage("CurseLit CurseLit CurseLit CurseLit CurseLit CurseLit CurseLit ");
  421.                     break;
  422.                 case "rules":
  423.                     irc.sendChatMessage("Rules: Unscramble the word before anyone else. If you guess correctly you get 10 points! You guess by doing !guess [word]. Good luck!");
  424.                     break;
  425.                 default:
  426.                     break;
  427.                 case "contact":
  428.                     irc.sendChatMessage("You can contact me through snapchat: will_miles11, Twitter: @zexodious, or even on youtube: https://youtube.com/zExodious");
  429.                     break;
  430.                 case "uptime":
  431.                     WebClient client = new WebClient();
  432.                     result = client.DownloadString(url);
  433.                     if (result != null)
  434.                     {
  435.                         irc.sendChatMessage(result);
  436.                     }
  437.                     else
  438.                     {
  439.                         irc.sendChatMessage("Something went wrong");
  440.                     }
  441.                     break;
  442.                 case "viewers":
  443.                     string modnames = "";
  444.                     string viewernames = "";
  445.                     Viewercount(ref modnames, ref viewernames);
  446.                     irc.sendChatMessage("Moderators: " + modnames);
  447.                     irc.sendChatMessage("Viewers: " + viewernames);
  448.                     break;
  449.  
  450.             }
  451.         }
  452.  
  453.  
  454.     }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement