Python 3.x 使用Python 3的Twitch机器人

Python 3.x 使用Python 3的Twitch机器人,python-3.x,bots,twitch,Python 3.x,Bots,Twitch,我搜索了很多地方,包括Twitch开发者网站,到目前为止还没有关于如何修复这个错误的答案。(是的,这是使用Python2.x编写的,我现在使用的是3.x,但我发现任何错误都可能很容易由我自己解决……不) 我在聊天中看到TwitchBot\utils.py“,第17行 send(“PRIVMSG{}:{}\r\n.format(cfg.CHAN,msg)) TypeError:需要类似字节的对象,而不是“str” 以下是我所拥有的: # utils.py # utility functions

我搜索了很多地方,包括Twitch开发者网站,到目前为止还没有关于如何修复这个错误的答案。(是的,这是使用Python2.x编写的,我现在使用的是3.x,但我发现任何错误都可能很容易由我自己解决……不)

我在聊天中看到TwitchBot\utils.py“,第17行 send(“PRIVMSG{}:{}\r\n.format(cfg.CHAN,msg)) TypeError:需要类似字节的对象,而不是“str”

以下是我所拥有的:

# 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
这是我为我的机器人准备的

# 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如果在这一点上对您有用,但您没有在第17行的UTF-8中编码您的消息,这就是导致您出错的原因。

IDK如果在这一点上对您有用,但您没有在第17行的UTF-8中编码您的消息,这就是导致您出错的原因