· 9 years ago · Nov 02, 2016, 08:18 AM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using System.Net;
11using System.Net.Sockets;
12using System.IO;
13using System.Threading;
14using System.Text.RegularExpressions;
15using System.Runtime.InteropServices;
16using System.Media;
17using System.Diagnostics;
18
19namespace phantbot
20{
21 public partial class Phantbot : Form
22 {
23 #region Variables
24
25 private static string userName = "phantbot";
26
27 private static string password = "oauth:its my oauthtoken";
28
29 IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
30 NetworkStream serverStream = default(NetworkStream);
31 string readData = "";
32 Thread chatThread;
33
34 #endregion
35
36 public Phantbot()
37 {
38 InitializeComponent();
39 }
40 private void Form1_Load(object sender, EventArgs e)
41 {
42 irc.joinRoom("phantomora");
43 chatThread = new Thread(getMessage);
44 chatThread.Start();
45 }
46 private void Phantbot_FormClosing(object sender, FormClosingEventArgs e)
47 {
48 irc.leaveRoom();
49 serverStream.Dispose();
50 Environment.Exit(0);
51 }
52
53 private void getMessage()
54 {
55 serverStream = irc.tcpClient.GetStream();
56 int buffsize = 0;
57 byte[] inStream = new byte[10025];
58 buffsize = irc.tcpClient.ReceiveBufferSize;
59 while (true)
60 {
61 try
62 {
63 readData = irc.readMessage();
64 msg();
65 }
66 catch (Exception e)
67 {
68
69 }
70 }
71 }
72 private void msg() //This is where everything is dealt with in chat. Commands, automatic timeouts, ect.
73 {
74 if (this.InvokeRequired)
75 this.Invoke(new MethodInvoker(msg));
76 else
77 {
78 string[] separator = new string[] { "#phantomora :" }; //channel
79 string[] singlesep = new string[] { ":","!" };
80 if (readData.Contains("PRIVMSG"))
81 {
82 string username = readData.Split(singlesep, StringSplitOptions.None)[1];
83 string message = readData.Split(separator, StringSplitOptions.None)[1];
84
85 if (message[0] == '!') commands(username, message);
86
87 chatBox.Text = chatBox.Text + username + " :" + message + Environment.NewLine;
88 }
89 //chatBox.Text = chatBox.Text + readData.ToString() + Environment.NewLine;
90 }
91 }
92
93 private void commands(string username, string message)
94 {
95 string command = message.Split(new[] { ' ', '!' }, StringSplitOptions.None)[1]; //commands
96
97 switch(command.ToLower())
98 {
99 case "whoisphantom":
100 irc.sendChatMessage("phantom is phantom");
101 break;
102 }
103 }
104
105 }
106
107 class IrcClient
108 {
109 private string userName;
110 private string channel;
111
112 public TcpClient tcpClient;
113 private StreamReader inputStream;
114 private StreamWriter outputStream;
115
116 public IrcClient(string ip, int port, string userName, string password)
117 {
118 tcpClient = new TcpClient(ip, port);
119 inputStream = new StreamReader(tcpClient.GetStream());
120 outputStream = new StreamWriter(tcpClient.GetStream());
121
122 outputStream.WriteLine("Pass " + password);
123 outputStream.WriteLine("NICK " + userName);
124 outputStream.WriteLine("USER " + userName + " 8 " + userName);
125 outputStream.WriteLine("CAP REQ :twitch.tv/membership");
126 outputStream.WriteLine("CAP REQ :twitch.tv/commands");
127 outputStream.Flush();
128 }
129
130 public void joinRoom(string channel)
131 {
132 this.channel = channel;
133 outputStream.WriteLine("JOIN #" + channel);
134 outputStream.Flush();
135
136 }
137
138 public void leaveRoom()
139 {
140 outputStream.Close();
141 inputStream.Close();
142 }
143
144 public void sendIrcMessage(string message)
145 {
146 outputStream.WriteLine(message);
147 outputStream.Flush();
148 }
149
150 public void sendChatMessage(string message)
151 {
152 sendIrcMessage(":" + userName + "!" + userName + "@" + userName + "tmi.twitch.tv PRIVMSG #" + channel + ":" + message);
153 }
154
155 public void PingResponse()
156 {
157 sendIrcMessage("PONG tmi.twitch.tv\r\n");
158 }
159
160 public string readMessage()
161 {
162 string message = "";
163 message = inputStream.ReadLine();
164 return message;
165 }
166 }
167}