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 Utils.py: line 17, in chat
sock.send(“PRIVMSG #{} :{}\r\n”.format(cfg.CHAN, msg))
TypeError: a bytes-like object is required, not ‘str’
Bot.py
# bot.py
# 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+ :")
utils.chat(s, "FUZZY Bot! Pew Pew, Ping Ping")
_thread.start_new_thread(utils.threadFillOpList, ())
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"))
else:
username = re.search(r"\w+", response).group(0)
message = CHAT_MSG.sub("", response)
print(response)
sleep(1)
if __name__ == "__main__":
main()
--------------------------------------------------------------------------------
Utils.py
# utils.py
# utility functions
import cfg
import urllib3, json
import time, _thread
from time import sleep
# send a chat message to server
#parameters:
# 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:
try:
url = "http://tmi.twitch.tv/group/user/fuzzybuttgaming/chatters"
req = urllib3.Request(url, headers={"accept": "*/*"})
response = urllib3.urlopen(req).read()
if response.find("502 Bad Gateway") == -1:
cfg.oplist.clear()
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"
except:
"do nothing"
sleep(5)
def isOp(user):
return user in cfg.oplist