Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Coded by ShadowTzu
- 'Free to use
- 'Website: http://tzu3d.weebly.com
- 'Youtube: https://www.youtube.com/user/shadowtzu
- 'Twitter: https://twitter.com/shadowtzu
- 'Facebook: https://www.facebook.com/Tzu3d
- 'Twitch: http://www.twitch.tv/shadowtzu
- 'my 3D engine, Tzu3D: http://shadowtzu.free.fr
- 'Example:
- ' Private mTwitchChat As TwitchChat
- ' [...]
- ' mTwitchChat = New TwitchChat("username", "oauth:TOKEN", "#shadowtzu", AddressOf Communicate)
- ' mTwitchChat.Connect()
- '
- ' [...]
- '
- ' Public Function Communicate(user As String, Message As String) As String
- ' Console.WriteLine("User '" & user & "' Say: " & Message)
- ' if you return a string, your bot say it in the chat
- ' Return String.Empty
- ' End Function
- Imports System.Net
- Imports System.Text
- Imports System.Net.Sockets
- ''' <summary>
- ''' Connecting to Twitch Chat (IRC protocol)
- ''' </summary>
- ''' <remarks></remarks>
- Public Class TwitchChat
- Private WithEvents mSocket As SocketClient
- Private mNick As String
- Private mPass As String
- Private mPort As Integer
- Private mIp As String
- Private mChannel As String
- Private IsWaitingConnected As Boolean = True
- Public Structure struct_Message
- Public Who As String
- Public User As String
- Public Code As String
- Public Message As String
- End Structure
- Public Delegate Function User_Message(User As String, Message As String) As String
- Private mCom As User_Message
- ''' <summary>
- ''' Constructor
- ''' </summary>
- ''' <param name="Nick">username</param>
- ''' <param name="OAuth_token">OAuth token can be get here http://www.twitchapps.com/tmi </param>
- ''' <param name="Channel">channel : #channelname</param>
- ''' <param name="CallBack_Communicate">create a callback function: Public Function Communicate(User As String, Message As String) As String ... End Function and give address: AddressOf Communicate</param>
- ''' <remarks></remarks>
- Public Sub New(Nick As String, OAuth_token As String, Channel As String, CallBack_Communicate As User_Message)
- mIp = "irc.twitch.tv"
- mPort = 6667
- mPass = OAuth_token
- mNick = Nick
- mChannel = Channel
- mCom = CallBack_Communicate
- mSocket = New SocketClient()
- End Sub
- Public Sub Connect()
- mSocket.Connect(mIp, mPort)
- End Sub
- Public Sub Write(Data As String)
- mSocket.Write(Data & vbCrLf)
- End Sub
- Public Sub Say(Message As String)
- If Message = "" Then Exit Sub
- Write(String.Format("PRIVMSG {0} :{1}", mChannel, Message))
- End Sub
- Public Sub Quit(Message As String)
- Write(String.Format("QUIT :{0}", Message))
- End Sub
- Private Sub Console_WriteLine(Message As String)
- If Not Debug Then Exit Sub
- Console.WriteLine(Message)
- End Sub
- Private Sub NetworkCom(Stream As String)
- Dim data() As String = Split(Stream, vbCrLf)
- For i As Integer = 0 To data.Count - 1
- Analyse_Stream(data(i))
- Next
- End Sub
- Private Rep As struct_Message
- Private Viewers_list As String = Nothing
- Private End_List_Receive As Boolean = False
- Private Sub Analyse_Stream(stream As String)
- If stream = "" Then Exit Sub
- Rep = Decode(stream)
- Select Case Rep.Code.ToUpper
- Case "353"
- Dim Viewers As String = Split(Rep.Message, ":")(1)
- Viewers_list &= Viewers
- Case "366"
- Console_WriteLine("Viewers List: " & Viewers_list)
- End_List_Receive = True
- Case "PING"
- Write("PONG " & Clean_Message(Rep.Message))
- Case "PRIVMSG"
- Say(mCom(Rep.User, Clean_Message(Rep.Message)))
- Case "JOIN"
- If End_List_Receive Then
- Say("Hello!")
- End If
- End Select
- End Sub
- Private Function Decode(line As String) As struct_Message
- Dim Msg As struct_Message = Nothing
- If line = "" Then Return Msg
- Dim splitted() As String = Split(line, " ")
- Msg.Who = splitted(0)
- Dim EndName As Integer = Msg.Who.IndexOf("!")
- If EndName > -1 Then
- Msg.User = Clean_Message(Msg.Who.Substring(0, EndName))
- End If
- Msg.Code = splitted(1)
- For i As Integer = 2 To splitted.Count - 1
- Msg.Message &= splitted(i)
- If i < splitted.Count - 1 Then Msg.Message &= " "
- Next
- Return Msg
- End Function
- Private Function Clean_Message(message As String) As String
- If message.Contains(":") Then
- Return Split(message, ":")(1)
- Else
- Return message
- End If
- End Function
- #Region "Event"
- Private Sub mSocket_OnClose() Handles mSocket.OnClose
- Console_WriteLine("Closed!")
- End Sub
- Private Sub mSocket_OnCloseFailed(Exception As Exception) Handles mSocket.OnCloseFailed
- Console_WriteLine(Exception.Message)
- End Sub
- Private Sub mSocket_OnConnect() Handles mSocket.OnConnect
- Console_WriteLine("Connected!")
- mSocket.Write(String.Format("PASS {0} {1}", mPass, vbCrLf))
- mSocket.Write("NICK " & mNick.ToLower & vbCrLf)
- mSocket.Write("JOIN #shadowtzu" & vbCrLf)
- IsWaitingConnected = False
- End Sub
- Private Sub mSocket_OnConnectFailed(Exception As Exception) Handles mSocket.OnConnectFailed
- Console_WriteLine(Exception.Message)
- End Sub
- Private Sub mSocket_OnRead(Stream As String) Handles mSocket.OnRead
- NetworkCom(Stream)
- End Sub
- Private Sub mSocket_OnReadFailed(Exception As Exception) Handles mSocket.OnReadFailed
- Console_WriteLine(Exception.Message)
- End Sub
- Private Sub mSocket_OnWrite() Handles mSocket.OnWrite
- Console_WriteLine("Send data")
- End Sub
- Private Sub mSocket_OnWriteFailed(Exception As Exception) Handles mSocket.OnWriteFailed
- Console_WriteLine(Exception.Message)
- End Sub
- #End Region
- #Region "Properties"
- Public ReadOnly Property Connected As Boolean
- Get
- Return mSocket.IsConnected Or IsWaitingConnected
- End Get
- End Property
- Public Property Debug As Boolean
- #End Region
- End Class
- ''' <summary>
- ''' Socket class, I don't write it, I do not know where I found this code
- ''' </summary>
- ''' <remarks></remarks>
- Public Class SocketClient
- Implements IDisposable
- #Region "Declares"
- Private TcpClient As TcpClient
- Private ReadBuffer(1023) As Byte
- Private SyncInvoke As System.ComponentModel.ISynchronizeInvoke
- #End Region
- #Region "Events"
- Public Event OnConnect()
- Public Event OnConnectFailed(ByVal Exception As Exception)
- Public Event OnWrite()
- Public Event OnWriteFailed(ByVal Exception As Exception)
- Public Event OnRead(ByVal Stream As String)
- Public Event OnReadFailed(ByVal Exception As Exception)
- Public Event OnClose()
- Public Event OnCloseFailed(ByVal Exception As Exception)
- #End Region
- #Region "Constructors"
- Public Sub New(Optional ByVal Sync As System.ComponentModel.ISynchronizeInvoke = Nothing)
- TcpClient = New TcpClient
- SyncInvoke = Sync
- End Sub
- Public Sub New(ByVal TcpClient As TcpClient, Optional ByVal Sync As System.ComponentModel.ISynchronizeInvoke = Nothing)
- Me.TcpClient = TcpClient
- SyncInvoke = Sync
- BeginRead()
- End Sub
- #End Region
- #Region "Functions"
- Public Sub Connect(ByVal Ip As String, ByVal Port As Integer)
- BeginConnect(Ip, Port)
- End Sub
- Public Sub Write(ByVal Data As String)
- BeginWrite(Data)
- End Sub
- Public Sub Close()
- If (TcpClient Is Nothing) Then Exit Sub
- Try
- TcpClient.Close()
- TcpClient = New TcpClient
- OnCloseInvoker()
- Catch Exception As Exception
- OnCloseFailedInvoker(Exception)
- End Try
- End Sub
- #End Region
- #Region "Begins"
- Private Sub BeginConnect(ByVal Ip As String, ByVal Port As Integer)
- If (TcpClient Is Nothing) Then Exit Sub
- Try
- TcpClient.BeginConnect(Ip, Port, AddressOf ConnectCallBack, Nothing)
- Catch Exception As Exception
- OnConnectFailedInvoker(Exception)
- End Try
- End Sub
- Private Sub BeginRead()
- If (TcpClient Is Nothing) OrElse (NetworkStream Is Nothing) OrElse (Not IsConnected) OrElse (Not NetworkStream.CanRead) Then Exit Sub
- Try
- NetworkStream.BeginRead(ReadBuffer, 0, ReadBuffer.Length, AddressOf ReadCallBack, Nothing)
- Catch Exception As Exception
- OnReadFailedInvoker(Exception)
- End Try
- End Sub
- Private Sub BeginWrite(ByVal Data As String)
- If (TcpClient Is Nothing) OrElse (NetworkStream Is Nothing) OrElse (Not IsConnected) OrElse (Not NetworkStream.CanWrite) Then Exit Sub
- Try
- Dim WriteBuffer() As Byte = Encoding.ASCII.GetBytes(Data)
- NetworkStream.BeginWrite(WriteBuffer, 0, WriteBuffer.Length, AddressOf WriteCallBack, Nothing)
- Catch Exception As Exception
- OnWriteFailedInvoker(Exception)
- End Try
- End Sub
- #End Region
- #Region "CallBacks"
- Private Sub ConnectCallBack(ByVal IAsyncResult As IAsyncResult)
- If (TcpClient Is Nothing) OrElse (IAsyncResult Is Nothing) Then Exit Sub
- Try
- TcpClient.EndConnect(IAsyncResult)
- BeginRead()
- OnConnectInvoker()
- Catch Exception As Exception
- OnConnectFailedInvoker(Exception)
- End Try
- End Sub
- Private Sub ReadCallBack(ByVal IAsyncResult As IAsyncResult)
- If (TcpClient Is Nothing) OrElse (NetworkStream Is Nothing) OrElse (IAsyncResult Is Nothing) OrElse (Not IsConnected) Then Exit Sub
- Try
- Dim ReadBytes As Integer = NetworkStream.EndRead(IAsyncResult)
- If ReadBytes > 0 Then
- Dim Stream As String = Encoding.Default.GetString(ReadBuffer).Replace(Chr(0), String.Empty)
- Array.Clear(ReadBuffer, 0, ReadBuffer.Length - 1)
- BeginRead()
- OnReadInvoker(Stream)
- Else
- Close()
- End If
- Catch Exception As Exception
- OnReadFailedInvoker(Exception)
- End Try
- End Sub
- Private Sub WriteCallBack(ByVal IAsyncResult As IAsyncResult)
- If (TcpClient Is Nothing) OrElse (NetworkStream Is Nothing) OrElse (IAsyncResult Is Nothing) OrElse (Not IsConnected) Then Exit Sub
- Try
- NetworkStream.EndWrite(IAsyncResult)
- OnWriteInvoker()
- Catch Exception As Exception
- OnWriteFailedInvoker(Exception)
- End Try
- End Sub
- #End Region
- #Region "Inter-Threading"
- Private Delegate Sub DelegateOnConnectHandler()
- Private Delegate Sub DelegateOnConnectFailedHandler(ByVal Exception As Exception)
- Private Delegate Sub DelegateOnWriteHandler()
- Private Delegate Sub DelegateOnWriteFailedHandler(ByVal Exception As Exception)
- Private Delegate Sub DelegateOnReadHandler(ByVal Stream As String)
- Private Delegate Sub DelegateOnReadFailedHandler(ByVal Exception As Exception)
- Private Delegate Sub DelegateOnCloseHandler()
- Private Delegate Sub DelegateOnCloseFailedHandler(ByVal Exception As Exception)
- Private DelegateOnConnect As New DelegateOnConnectHandler(AddressOf RaiseOnConnectEvent)
- Private DelegateOnConnectFailed As New DelegateOnConnectFailedHandler(AddressOf RaiseOnConnectFailedEvent)
- Private DelegateOnWrite As New DelegateOnWriteHandler(AddressOf RaiseOnWriteEvent)
- Private DelegateOnWriteFailed As New DelegateOnWriteFailedHandler(AddressOf RaiseOnWriteFailedEvent)
- Private DelegateOnRead As New DelegateOnReadHandler(AddressOf RaiseOnReadEvent)
- Private DelegateOnReadFailed As New DelegateOnReadFailedHandler(AddressOf RaiseOnReadFailedEvent)
- Private DelegateOnClose As New DelegateOnCloseHandler(AddressOf RaiseOnCloseEvent)
- Private DelegateOnCloseFailed As New DelegateOnCloseFailedHandler(AddressOf RaiseOnCloseFailedEvent)
- Private Sub RaiseOnConnectEvent()
- RaiseEvent OnConnect()
- End Sub
- Private Sub RaiseOnConnectFailedEvent(ByVal Exception As Exception)
- RaiseEvent OnConnectFailed(Exception)
- End Sub
- Private Sub RaiseOnWriteEvent()
- RaiseEvent OnWrite()
- End Sub
- Private Sub RaiseOnWriteFailedEvent(ByVal Exception As Exception)
- RaiseEvent OnWriteFailed(Exception)
- End Sub
- Private Sub RaiseOnReadEvent(ByVal Stream As String)
- RaiseEvent OnRead(Stream)
- End Sub
- Private Sub RaiseOnReadFailedEvent(ByVal Exception As Exception)
- RaiseEvent OnReadFailed(Exception)
- End Sub
- Private Sub RaiseOnCloseEvent()
- RaiseEvent OnClose()
- End Sub
- Private Sub RaiseOnCloseFailedEvent(ByVal Exception As Exception)
- RaiseEvent OnCloseFailed(Exception)
- End Sub
- Private Sub OnConnectInvoker()
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnConnect, Nothing)
- Else
- RaiseOnConnectEvent()
- End If
- End Sub
- Private Sub OnConnectFailedInvoker(ByVal Exception As Exception)
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnConnectFailed, New Object() {Exception})
- Else
- RaiseOnConnectFailedEvent(Exception)
- End If
- End Sub
- Private Sub OnWriteInvoker()
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnWrite, Nothing)
- Else
- RaiseOnWriteEvent()
- End If
- End Sub
- Private Sub OnWriteFailedInvoker(ByVal Exception As Exception)
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnWriteFailed, New Object() {Exception})
- Else
- RaiseOnWriteFailedEvent(Exception)
- End If
- End Sub
- Private Sub OnReadInvoker(ByVal Stream As String)
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnRead, New Object() {Stream})
- Else
- RaiseOnReadEvent(Stream)
- End If
- End Sub
- Private Sub OnReadFailedInvoker(ByVal Exception As Exception)
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnReadFailed, New Object() {Exception})
- Else
- RaiseOnReadFailedEvent(Exception)
- End If
- End Sub
- Private Sub OnCloseInvoker()
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnClose, Nothing)
- Else
- RaiseOnCloseEvent()
- End If
- End Sub
- Private Sub OnCloseFailedInvoker(ByVal Exception As Exception)
- If (Not SyncInvoke Is Nothing) AndAlso SyncInvoke.InvokeRequired Then
- SyncInvoke.BeginInvoke(DelegateOnCloseFailed, New Object() {Exception})
- Else
- RaiseOnCloseFailedEvent(Exception)
- End If
- End Sub
- #End Region
- #Region "Properties"
- Public ReadOnly Property IsConnected As Boolean
- Get
- If (TcpClient Is Nothing) Then Return False
- Try
- Return TcpClient.Connected
- Catch Exception As Exception
- Return False
- End Try
- End Get
- End Property
- Public ReadOnly Property Ip As String
- Get
- If (TcpClient Is Nothing) Then Return Nothing
- Try
- Return CType(TcpClient.Client.RemoteEndPoint, IPEndPoint).Address.ToString
- Catch Exception As Exception
- Return Nothing
- End Try
- End Get
- End Property
- Public ReadOnly Property Port As Integer
- Get
- If (TcpClient Is Nothing) Then Return Nothing
- Try
- Return CType(TcpClient.Client.RemoteEndPoint, IPEndPoint).Port
- Catch Exception As Exception
- Return Nothing
- End Try
- End Get
- End Property
- Private ReadOnly Property NetworkStream As NetworkStream
- Get
- If (TcpClient Is Nothing) Then Return Nothing
- Try
- Return TcpClient.GetStream
- Catch Exception As Exception
- Return Nothing
- End Try
- End Get
- End Property
- #End Region
- #Region "IDisposable Support"
- Private disposedValue As Boolean
- Protected Overridable Sub Dispose(disposing As Boolean)
- If Not Me.disposedValue Then
- If disposing Then
- End If
- TcpClient.Close()
- TcpClient = Nothing
- Erase ReadBuffer
- ReadBuffer = Nothing
- SyncInvoke = Nothing
- End If
- Me.disposedValue = True
- End Sub
- Public Sub Dispose() Implements IDisposable.Dispose
- Dispose(True)
- GC.SuppressFinalize(Me)
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement