Python 3 Chat Bot Error

Good afternoon! I have tried looking at similar topics but could not find a clear answer, or one that I understood. I am using Python 3.x (the video i watched was for Python 2.x but figured maybe I could figure out any errors that popped up). The error so far is in the line 17, in chat
sock.send(“PRIVMSG #{} :{}\r\n”.format(cfg.CHAN, msg))
TypeError: a bytes-like object is required, not ‘str’
# The code for the bot

import cfg
import utils
import socket
import re
import time, _thread
from time import sleep

def main():
    # Network functions
    s = socket.socket()
    s.connect((cfg.HOST, cfg.PORT))
    s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8"))
    s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8"))
    s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8"))

    CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :"), "FUZZY Bot! Pew Pew, Ping Ping")

    _thread.start_new_thread(utils.threadFillOpList, ())

    while True:
        response = s.recv(1024).decode("utf-8")
        if response == "PING\r\n":
            username ="\w+", response).group(0)
            message = CHAT_MSG.sub("", response)


if __name__ == "__main__":
# utility functions

import cfg
import urllib3, json
import time, _thread
from time import sleep

# send a chat message to server
    # sock -- the socket over which to send the message
    # msg -- the message to send

def chat(sock, msg):
    sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg))

# function: ban
# ban user from channel
# Parameters:
#   sock -- the socket over which to send the ban command
#   user -- the user to be banned
def ban(sock, user):
    chat(sock, ".ban {}".format(user))

# Function: timeout
# Timeout user for a certain time
# Parameter:
#   sock -- socket over which to send the timeout command
#   user -- the user to be timed out
#   seconds -- the length of timeout (default 600

def timeout(sock, user, seconds=600):
    chat(sock, ".timeout {}".format(user, seconds))

# Function: thread/oplist
# In a separate list fill up the op list
def threadFillOpList():
    while True:
            url = ""
            req = urllib3.Request(url, headers={"accept": "*/*"})
            response = urllib3.urlopen(req).read()
            if response.find("502 Bad Gateway") == -1:
                data = json.loads(response)
                for p in data["chatters"]["moderators"]:
                    cfg.oplist[p] = "mod"
                for p in data["chatters"]["global_mods"]:
                    cfg.oplist[p] = "global_mod"
                for p in data["chatters"]["admins"]:
                    cfg.oplist[p] = "admin"
                for p in data["chatters"]["staff"]:
                    cfg.oplist[p] = "staff"
            "do nothing"

def isOp(user):
    return user in cfg.oplist

socket is low-level, try putting b before the string like this:

sock.send(b"PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg))

Or by using .encode("utf-8") like this:

sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg).encode("utf-8"))
1 Like

Awesome, Ill have to try it out, I downloaded Python 2.7 and everything launches. now trying to figure out how to send just regular messages through it. It connects but doesn’t respond to and !commands or show anything anyone types in chat. maybe Im typing messages in the wrong area or something

Same here. I used the same video as you did. And I can’t receive anything except of “first join log”. It just doesn’t react on messages I write in the chat. I can’t even print them to the console, so the problem is in the line “response = s.recv(1024).decode(“utf-8”)”. How to fix this?
In addition I must say that I can write anything I want to chat, but I still can’t read what the others write there.

You JOIN <cfg.CHAN>, but send with PRIVMSG #<cfg.CHAN>. Notice the #; it needs to be consistent for both: #<channelname>. Whether the # comes from the cfg.CHAN or the code doesn’t matter, but there must be exactly one # for the IRC commands.

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