Advertisement
Guest User

Code

a guest
May 23rd, 2017
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.02 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;
  11. using System.Net.Sockets;
  12. using System.IO;
  13. using System.Threading;
  14. using System.Text.RegularExpressions;
  15. using System.Runtime.InteropServices;
  16. using System.Media;
  17. using System.Diagnostics;
  18. using TwitchCSharp.Clients;
  19. using TwitchCSharp.Models;
  20.  
  21. namespace TestBot
  22. {
  23.     public partial class Form1 : Form
  24.     {
  25.         #region Variables
  26.         private static string TwitchClientID = "";
  27.         private static string userName = "";
  28.         private static string password = "";
  29.  
  30.  
  31.         TwitchReadOnlyClient APIClient = new TwitchReadOnlyClient(TwitchClientID);
  32.        
  33.  
  34.         IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
  35.         NetworkStream serverStream = default(NetworkStream);
  36.         string readData = "";
  37.         Thread chatThread;
  38.         List<string> BannedWords = new List<string> {/*Banned words here in quotes*/};
  39.  
  40.         bool commandSpamFilter = false;
  41.  
  42.         #endregion
  43.  
  44.         #region Form
  45.  
  46.         public Form1()
  47.         {
  48.             InitializeComponent();
  49.         }
  50.         private void Form1_Load(object sender, EventArgs e)
  51.         {
  52.             irc.joinRoom("bylimbo_");
  53.             chatThread = new Thread(getMessage);
  54.             chatThread.Start();
  55.  
  56.         }
  57.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  58.         {
  59.             irc.leaveRoom();
  60.             serverStream.Dispose();
  61.             Environment.Exit(0);
  62.         }
  63.         #endregion
  64.  
  65.         private void getMessage()
  66.         {
  67.             serverStream = irc.tcpClient.GetStream();
  68.             int buffsize = 0;
  69.             byte[] inStream = new byte[10025];
  70.             buffsize = irc.tcpClient.ReceiveBufferSize;
  71.             while (true)
  72.             {
  73.                 try
  74.                 {
  75.                     readData = irc.readMessage();
  76.                     msg();
  77.                 }
  78.                 catch (Exception e)
  79.                 {
  80.  
  81.                 }
  82.             }
  83.         }
  84.  
  85.         private void msg() //This is where everything is dealt with in chat, commands, automatic timeouts, etc.
  86.         {
  87.             if (this.InvokeRequired) this.Invoke(new MethodInvoker(msg));
  88.             else
  89.             {
  90.                 string[] separator = new string[] { "#bylimbo_ :" };
  91.                 string[] singlesep = new string[] { ":", "!" };
  92.  
  93.  
  94.                 if (readData.Contains("PRIVMSG"))
  95.                 {
  96.                     string userName = readData.Split(singlesep, StringSplitOptions.None)[1];
  97.                     string message = readData.Split(separator, StringSplitOptions.None)[1];
  98.  
  99.                     if (BannedWordFilters(userName, message)) return;
  100.  
  101.                     if (message[0] == '!') commands(userName, message);
  102.  
  103.                     chatBox.Text = chatBox.Text + userName + " : " + message + Environment.NewLine;
  104.  
  105.                     int num = chatBox.Lines.Count();
  106.                     if (chatBox.Lines.Count() > 501)
  107.                     {
  108.                         var foos = new List<string>(chatBox.Lines);
  109.                         foos.RemoveAt(0);
  110.                         chatBox.Lines = foos.ToArray();
  111.                     }
  112.                 }
  113.  
  114.                 if (readData.Contains("PING"))
  115.                 {
  116.                     irc.PingResponse();
  117.                 }
  118.             }
  119.         }
  120.  
  121.         private void commands(string userName, string message)
  122.         {
  123.             string command = message.Split(new[] { ' ', '!' }, StringSplitOptions.None)[1]; //!command
  124.  
  125.             switch (command.ToLower())
  126.             {
  127.  
  128.                 #region Schedule
  129.                 case "schedule":
  130.                     irc.sendChatMessage("My Schedule is Tuesday, Wednesday & Saturday 7:30PM - 10:30PM PST ");
  131.                     break;
  132.                 #endregion
  133.  
  134.                
  135.                 #region Dropped
  136.                 case "dropped":
  137.                     irc.sendChatMessage("PJSalt KAPOW BibleThump BibleThump");
  138.                     break;
  139.                 #endregion
  140.  
  141.                 #region Hype
  142.                 case "hype":
  143.                     irc.sendChatMessage("KAPOW CurseLit KAPOW CurseLit KAPOW CurseLit KAPOW CurseLit");
  144.                     break;
  145.                 #endregion
  146.  
  147.                 #region UpTime
  148.                 case "uptime":
  149.                     irc.sendChatMessage("");
  150.                     break;
  151.                 #endregion
  152.  
  153.                 #region Default
  154.                 default:
  155.                     irc.sendChatMessage("That is not a command");
  156.                     break;
  157.                 #endregion
  158.             }
  159.         }
  160.  
  161.         private bool BannedWordFilters(string username, string message)
  162.         {
  163.             foreach (string word in BannedWords)
  164.             {
  165.                 if (message.Contains(word))
  166.                 {
  167.                     string command = "/timeout " + userName + " 10";
  168.                     irc.sendChatMessage(command);
  169.                     irc.sendChatMessage(userName + " have been timed out for using a banned word.");
  170.                     return true;
  171.                 }
  172.             }
  173.             return false;
  174.         }
  175.  
  176.         private void chatBox_TextChanged(object sender, EventArgs e)
  177.         {
  178.             chatBox.SelectionStart = chatBox.Text.Length;
  179.             chatBox.ScrollToCaret();
  180.         }
  181.  
  182.         private void SendButton_Click(object sender, EventArgs e)
  183.         {
  184.             irc.sendChatMessage(BotChatBot.Text);
  185.             BotChatBot.Clear();
  186.         }
  187.  
  188.         private void BotChatBot_KeyPress(object sender, KeyPressEventArgs e)
  189.         {
  190.             if (e.KeyChar == Convert.ToChar(Keys.Return))
  191.             {
  192.                 irc.sendChatMessage(BotChatBot.Text);
  193.                 BotChatBot.Clear();
  194.             }
  195.         }
  196.     }
  197.     class IrcClient
  198.         {
  199.             private string userName;
  200.             private string channel;
  201.  
  202.             public TcpClient tcpClient;
  203.             private StreamReader inputStream;
  204.             private StreamWriter outputStream;
  205.  
  206.             public IrcClient(string ip, int port, string userName, string password)
  207.             {
  208.                 tcpClient = new TcpClient(ip, port);
  209.                 inputStream = new StreamReader(tcpClient.GetStream());
  210.                 outputStream = new StreamWriter(tcpClient.GetStream());
  211.  
  212.                 outputStream.WriteLine("PASS " + password);
  213.                 outputStream.WriteLine("NICK " + userName);
  214.                 outputStream.WriteLine("USER " + userName + " 8 * :" + userName);
  215.                 outputStream.WriteLine("CAP REQ :twitch.tv/membership");
  216.                 outputStream.WriteLine("CAP REQ :twitch.tv/commands");
  217.                 outputStream.Flush();
  218.             }
  219.  
  220.             public void joinRoom(string channel)
  221.             {
  222.                 this.channel = channel;
  223.                 outputStream.WriteLine("JOIN #" + channel);
  224.                 outputStream.Flush();
  225.             }
  226.  
  227.             public void leaveRoom()
  228.             {
  229.                 outputStream.Close();
  230.                 inputStream.Close();
  231.             }
  232.  
  233.             public void sendIrcMessage(string message)
  234.             {
  235.                 outputStream.WriteLine(message);
  236.                 outputStream.Flush();
  237.             }
  238.  
  239.             public void sendChatMessage(string message)
  240.             {
  241.                 sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :" + message);
  242.             }
  243.  
  244.             public void PingResponse()
  245.             {
  246.                 sendIrcMessage("PONG tmi.twitch.tv\r\n");
  247.             }
  248.  
  249.             public string readMessage()
  250.             {
  251.                 string message = "";
  252.                 message = inputStream.ReadLine();
  253.                 return message;
  254.             }
  255.         }
  256.  
  257.  
  258.  
  259.  
  260.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement