【问题标题】:Twitch Bot using Python 3使用 Python 3 的 Twitch 机器人
【发布时间】:2017-12-26 18:06:16
【问题描述】:

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

我在聊天中收到 TwitchBot\utils.py",第 17 行 sock.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 的内容

# 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()

【问题讨论】:

    标签: python-3.x bots twitch


    【解决方案1】:

    IDK,如果此时这对您有用,但您没有在第 17 行以 UTF-8 编码您的消息,这就是您的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 2018-12-31
      • 2014-08-26
      • 2015-02-09
      • 2019-05-08
      • 2014-09-18
      • 2014-08-12
      • 1970-01-01
      相关资源
      最近更新 更多