Search code examples
pythonpython-3.xirctwitch

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 1023: unexpected end of data


Basically I've made an IRC Twitch bot in Python that does nothing but join the channel for now. The ping-pong cycle works properly for a while but then it gets halted with the error in the title. What did I do wrong? Thanks in advance.

import re
import socket

HOST = "irc.twitch.tv"
PORT = 6667
NICK = "asdsad"
PASS = "oauth:asdasdasdasd"
channel = "#coolperson"

def send_message(sock, msg):
    sock.send("PRIVMSG #{} {}".format(channel, msg))

s = socket.socket()
s.connect((HOST, PORT))
s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
s.send("JOIN {}\r\n".format(channel).encode("utf-8"))

while True:
    response = s.recv(1024).decode("utf-8")
    if response == "PING :tmi.twitch.tv\r\n":
        s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
        print("answered the call")

Solution

  • You can skip that error. Instead of the following part:

    response = s.recv(1024).decode("utf-8")
    

    Use this one:

    response = s.recv(1024).decode('utf-8', 'ignore')