I have search numerous places including the Twitch Developers site and so far no answer to how to fix this error. (Yes this was written using Python 2.x and I'm using 3.x but figured any errors might be simple to figure out on my own...nope)
I am getting TwitchBot\utils.py", line 17, in chat sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg)) TypeError: a bytes-like object is required, not 'str'
Here is what I have:
# 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
Here is what I have for my 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)
# Custom Commands
if message.strip() == "!time":
utils.chat(s, "It is " + time.strftime("%I:%M %p %Z on %A, %B %d, %Y."))
if message.strip() == "!messages":
utils.chat(s, "Feel free to follow if you want, no hard feelings either way :D")
sleep(1)
if __name__ == "__main__":
main()
IDK if this is any use to you at this point, but you aren't encoding your message in UTF-8 on line 17, which is what's giving you your error.