Twitch keeps sending spaces to IRC Bot Python

after a certain amount of time my bot keeps recieving ’ ’ from the twitch servers
After almost a month or so, me and my friend still don’t know how to fix this.

This is “probably” the problem why i get spaces:

con = socket.socket()

con.connect((HOST, PORT))

con.send(bytes(‘CAP REQ\r\n’, ‘UTF-8’))

data = “”
while True:
ready =[con], [], [], 8)
if ready[0]:
data = data + con.recv(1024).decode(“UTF-8”)
logging.basicConfig(filename=‘crash.log’, level = logging.DEBUG)
if len(data) != 0:
data_split = re.split(r"[~\r\n+]", data)
data = data_split.pop()

If you need anything else please say so :slight_smile:

Just to clarify, this isn’t the PING/PONG messages that the server sends to keep the message alive?

i don’t think it’s the Ping but i might be wrong,
lately i doesn’t print Pong anymore
In case this is the problem
here is the ping/pong code:

if data == “PING\r\n”:
print(“Pong send!”)
sender = get_sender(line[1]).lower()
message = get_message(line[1:])

I have tried different Pong methodes with no success

I keep getting PING
but it doesn’t print “Pong send!”

First off, you should parse the IRC message properly. It’ll save quite a bit of trouble doing it right the first time around.

The second thing is, you only need to identify it as a PING, the trailing part ( is irrelevant, you’ll just stick it in your PONG reply.

Here’s a snippet from TwitchChatSharp (C#)

    var ircMsg = IrcMessage.FromRawMessage(message);

    MessageReceived(this, new IrcMessageEventArgs(ircMsg));

    switch (ircMsg.Command)
        case IrcCommand.Ping:
            EnqueueMessage(message.Replace("PING", "PONG"), true);
        case IrcCommand.Reconnect:

As a side-note, please surround code with triple backticks and it’ll show up properly:

// code here
if data == "PING":
     print("Pong send!")

Is this what you mean?

More accurate would be

if data.startswith("PING"):
    print("Pong sent!")

But I’ve used something as dirty as checking if the first character in data is P, because in Twitch chat only PING messages start with P (others start with a colon : or another letter)

Your code doesn’t seem to work

still getting only the ping but no Pong sent

Ok so i kinda know the problem now.
It doesn’t send a PONG because it can’t find the PING.
Any idea’s why?

Ok i fixed it :slight_smile:

i had to make a schedule timer to pong every 5 min

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