Bot can send messages, read from connection, but not receive messages

I have a bot that can connect to chat, and send messages, but not get anything from chat. Does anyone know why not? I’ve tried on my channel and on another, more active one but… nothing.

Output and VB .Net console app code below:


Output

< PASS oauth:TOKEN_GOES_HERE
< NICK pilchard123
< USER pilchard123 8 * :pilchard123
> :tmi.twitch.tv 001 pilchard123 :Welcome, GLHF!
> :tmi.twitch.tv 002 pilchard123 :Your host is tmi.twitch.tv
> :tmi.twitch.tv 003 pilchard123 :This server is rather new
> :tmi.twitch.tv 004 pilchard123 :-
> :tmi.twitch.tv 375 pilchard123 :-
> :tmi.twitch.tv 372 pilchard123 :You are in a maze of twisty passages, all alike.
> :tmi.twitch.tv 376 pilchard123 :>
< JOIN #pilchard123

Code

Imports System.IO
Imports System.Net.Sockets

Module Module1

Private Const TOKEN = "TOKEN_GOES_HERE"

Sub Main()

    Try
        MainAsync().GetAwaiter().GetResult()
    Catch ex As Exception
        Debugger.Break()
    End Try


    Debugger.Break()
    Console.WriteLine("Done!")
    Console.ReadLine()
End Sub

Public Async Function MainAsync() As Task

    Using client = New TwitchIrcClient

        client.AddSendListener(AddressOf PrintSentMessage)
        client.AddReceiveListener(AddressOf PrintReceivedMessage)

        client.SendMessage($"PASS oauth:{TOKEN}")
        client.SendMessage($"NICK pilchard123")
        client.SendMessage($"USER pilchard123 8 * :pilchard123")
        client.GetAllMessages()

        client.SendMessage($"JOIN #pilchard123")

        While True
            client.GetAllMessages()
            Await Task.Delay(100)
        End While


    End Using

    Debugger.Break()

    Await Task.Delay(-1)

End Function

Private Sub PrintSentMessage(message As String)
    Console.WriteLine($"< {message}")
End Sub

Private Sub PrintReceivedMessage(message As String)
    Console.WriteLine($"> {message}")
End Sub

End Module


Class TwitchIrcClient
Implements IDisposable

Private tcpClient As TcpClient
Private reader As StreamReader
Private writer As StreamWriter

Private SendListener As New List(Of Action(Of String))
Public Sub AddSendListener(listener As Action(Of String))
    SendListener.Add(listener)
End Sub
Public Sub RemoveSendListener(listener As Action(Of String))
    SendListener.Remove(listener)
End Sub

Private ReceiveListener As New List(Of Action(Of String))
Public Sub AddReceiveListener(listener As Action(Of String))
    ReceiveListener.Add(listener)
End Sub
Public Sub RemoveReceiveListener(listener As Action(Of String))
    ReceiveListener.Remove(listener)
End Sub

Public Sub New()
    tcpClient = New TcpClient("irc.chat.twitch.tv", 6667)
    reader = New StreamReader(tcpClient.GetStream)
    writer = New StreamWriter(tcpClient.GetStream)
End Sub

Public Sub SendMessage(message As String)
    writer.Flush()
    writer.WriteLine(message)
    writer.Flush()
    For Each l In SendListener
        l(message)
    Next
End Sub

Public Function HasMessage() As Boolean
    Return reader.Peek() >= 0
End Function

Public Function GetNextMessage() As String
    If HasMessage() Then
        Dim message = reader.ReadLine()
        For Each l In ReceiveListener
            l(message)
        Next l
        If message = "PING :tmi.twitch.tv" Then
            SendMessage("PONG :tmi.twitch.tv")
        End If
        Return message
    Else
        Return Nothing
    End If
End Function

Public Function GetAllMessages() As IEnumerable(Of String)
    Dim retVal = New List(Of String)
    While HasMessage()
        retVal.Add(GetNextMessage)
    End While
    Return retVal
End Function

Private disposedValue As Boolean

Protected Overridable Sub Dispose(disposing As Boolean)
    If Not disposedValue Then
        If disposing Then
            reader.Dispose()
            writer.Dispose()
            tcpClient.Close()
        End If
    End If
    disposedValue = True
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
End Sub
End Class

Sorry I don’t really know the programming language, but be careful about posting oauth tokens publicly (in your log output), they need to be kept private. I suggest you revoke access to that immediately if it’s valid.

2 Likes

And I was so careful to remove it from the code as well…

Thanks for the heads up!

When you send are you doing

PRIVMSG #room words to send

Syntax above wrong as on mobile. But I see no PRIVMSG in your code

Yeah, I use PRIVMSG to send. That works okay; the message shows up fine on the website. I just included the minimum code to reproduce the behaviour.

Or lack of it, as the case may be.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.