Advertisement
miltonthepic

C# ChatBot Broken

Nov 9th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 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.IO;
  7. using System.Linq;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace TwitchChatBot
  15. {
  16.     public partial class mainWindow : Form
  17.     {
  18.         #region variables
  19.         private static string userName = "amg_bot";
  20.         private static string password = "oauth:someotherbsafter";
  21.  
  22.         IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
  23.         NetworkStream serverStream = default(NetworkStream);
  24.         string readData = "";
  25.         Thread chatThread;
  26.         #endregion
  27.  
  28.         public mainWindow()
  29.         {
  30.             //Initialization of the Windows Forms Components
  31.             InitializeComponent();
  32.         }
  33.  
  34.         private void mainWindow_Load(object sender, EventArgs e)
  35.         {
  36.             irc.joinRoom("officialantimattergaming");
  37.             chatThread = new Thread(getMessage);
  38.             chatThread.Start();
  39.         }
  40.  
  41.         private void getMessage()
  42.         {
  43.             serverStream = irc.tcpClient.GetStream();
  44.             int buffSize = 0;
  45.             byte[] intStream = new byte[10025];
  46.             buffSize = irc.tcpClient.ReceiveBufferSize;
  47.  
  48.             while (true)
  49.             {
  50.                 try
  51.                 {
  52.                     readData = irc.readMessage();
  53.                     msg();
  54.                 }
  55.                 catch (Exception e) { }
  56.             }
  57.         }
  58.  
  59.         private void msg()
  60.         {
  61.             if(this.InvokeRequired)
  62.             {
  63.                 this.Invoke(new MethodInvoker(msg));
  64.             }
  65.             else
  66.             {
  67.                 chatWindow.Text = chatWindow.Text + readData.ToString() + Environment.NewLine;
  68.             }
  69.         }
  70.     }
  71.  
  72.     class IrcClient
  73.     {
  74.         private string userName;
  75.         private string channel;
  76.  
  77.  
  78.         public TcpClient tcpClient;
  79.         private StreamReader inputStream;
  80.         private StreamWriter outputStream;
  81.  
  82.         public IrcClient(string ip, int port, string userName, string password)
  83.         {
  84.             tcpClient = new TcpClient(ip, port);
  85.             inputStream = new StreamReader(tcpClient.GetStream());
  86.             outputStream = new StreamWriter(tcpClient.GetStream());
  87.  
  88.             outputStream.WriteLine("PASS ", password);
  89.             outputStream.WriteLine("NICK ", userName);
  90.             outputStream.WriteLine("USER ", userName,  " 8 * :");
  91.             outputStream.WriteLine("CAP REQ :twitch.tv/commands");
  92.             outputStream.WriteLine("CAP REQ :twitch.tv/membership");
  93.             outputStream.Flush();
  94.         }
  95.  
  96.         public void joinRoom(string channel)
  97.         {
  98.             this.channel = channel;
  99.  
  100.             outputStream.WriteLine("JOIN #" + channel);
  101.             outputStream.Flush();
  102.         }
  103.  
  104.         public void leaveRoom()
  105.         {
  106.             outputStream.Close();
  107.             inputStream.Close();
  108.         }
  109.  
  110.         public void sendIrcMessage(string message)
  111.         {
  112.             outputStream.WriteLine(message);
  113.             outputStream.Flush();
  114.         }
  115.        
  116.         public void sendChatMessage(string message)
  117.         {
  118.             sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :" + message);
  119.         }
  120.  
  121.         public void pingResponse()
  122.         {
  123.             sendIrcMessage("PONG tmi.twitch.tv\r\n");
  124.         }
  125.  
  126.         public string readMessage()
  127.         {
  128.             string message = "";
  129.             message = inputStream.ReadLine();
  130.             return message;
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement