-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitchClientIrc.cs
118 lines (98 loc) · 3.17 KB
/
TwitchClientIrc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Net.Sockets;
using System.IO;
namespace TwitchBotFramework
{
public class TwitchClientIrc
{
public string UserName;
public string ChannelName;
private TcpClient _tcpClient;
private StreamReader _streamIn;
private StreamWriter _streamOut;
private string _sendString;
public delegate void standardeventdelegate(MessageEventArgs e);
public static event standardeventdelegate ChatMessage;
public TwitchClientIrc(string url, int port, string username, string password, string channel)
{
try
{
UserName = username;
ChannelName = channel;
Console.WriteLine("Connecting to tcp client");
_tcpClient = new TcpClient(url, port);
_streamIn = new StreamReader(_tcpClient.GetStream());
_streamOut = new StreamWriter(_tcpClient.GetStream());
_sendString = ":" + UserName + "!" + UserName + "@" + UserName + ".tmi.twitch.tv PRIVMSG #" + ChannelName + " :";
_streamOut.WriteLine("PASS " + password);
_streamOut.WriteLine("NICK " + username);
_streamOut.WriteLine("USER " + username + " 8 * :" + username);
_streamOut.WriteLine("JOIN #" + channel);
_streamOut.Flush();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void Send(string m)
{
try
{
_streamOut.WriteLine(m);
_streamOut.Flush();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public void SendChatMessage(string m)
{
Send(_sendString + m);
}
private string Read()
{
try
{
string m = _streamIn.ReadLine();
return m;
}
catch (System.Net.Sockets.SocketException e)
{
Console.WriteLine("Error reading input stream" + e);
return "";
}
}
public void ReadMessage()
{
try
{
string m = Read();
//Console.WriteLine(m);
if (m.Contains("PRIVMSG"))
{
int delimiterIndex = m.IndexOf('!');
string senderUserName = m.Substring(1, delimiterIndex - 1);
delimiterIndex = m.IndexOf(" :");
string senderMessage = m.Substring(delimiterIndex + 2);
ChatMessage?.Invoke(new MessageEventArgs(senderMessage, senderUserName));
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
public class MessageEventArgs : EventArgs
{
public string Message;
public string Sender;
public MessageEventArgs(string message, string sender)
{
Message = message;
Sender = sender;
}
}
}