抽搐机器人一个字节类似的对象是必需的,而不是'str'



大家好,我正在尝试学习如何为我的朋友的频道制作一个Twitch Bot,它们会流式传播监管,有时我会加入他们。现在,我能够将机器人加入聊天室,但我无法弄清楚管理部门,因此允许的机器人可以超时使用" Swear"一词相反,我得到了这个错误:

tmi: :tmi.twitch.tv 001 wasddabulyuasede_bot :Welcome, GLHF!
:tmi.twitch.tv 002 wasddabulyuasede_bot :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 wasddabulyuasede_bot :This server is rather new
:tmi.twitch.tv 004 wasddabulyuasede_bot :-
:tmi.twitch.tv 375 wasddabulyuasede_bot :-
:tmi.twitch.tv 372 wasddabulyuasede_bot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 wasddabulyuasede_bot :>
wasddabulyuasede_bot: :wasddabulyuasede_bot!wasddabulyuasede_bot@wasddabulyuasede_bot.tmi.twitch.tv JOIN #wlrs
_
:wasddabulyuasede_bot.tmi.twitch.tv 353 wasddabulyuasede_bot = #wlrs_ :wasddabulyuasede_bot
:wasddabulyuasede_bot.tmi.twitch.tv 366 wasddabulyuasede_bot #wlrs_ :End of /NAMES list
wlrs_: swear
Traceback (most recent call last):
  File "Bot.py", line 57, in <module>
    timeout(s,username,10)
  File "Bot.py", line 33, in timeout
    chat(sock, ".timeout {}".format(user, secs))
  File "Bot.py", line 14, in chat
    sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg_encoded))
TypeError: a bytes-like object is required, not 'str'

代码

#cfg.py
#oauth key has been removed
HOST = "irc.chat.twitch.tv"
PORT = 6667
PASS = "oauth:xxxxxxxxxxxxxxxxxxxx"
NICK = "wasddabulyuasede_bot"
CHAN = "#wlrs_"
RATE = (20/30)
PATT = [
    r"swear"
]

Bot.py
from cfg import HOST, PORT, PASS, NICK, CHAN, RATE, PATT
import cfg
import socket
import re
import time
def chat(sock, msg):
    """
    Send a chat message to the server.
    Keyword arguments:
    sock -- the socket over which to send the message
    msg  -- the message to be sent
    """
    msg_encoded = msg.encode("utf-8")
    sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg_encoded))
# def ban(sock, user):
#     """
#     Ban a user from the current channel.
#     Keyword arguments:
#     sock -- the socket over which to send the ban command
#     user -- the user to be banned
#     """
#     chat(sock, ".ban {}".format(user))
#
def timeout(sock, user, secs=600):
    """
    Time out a user for a set period of time.
    Keyword arguments:
    sock -- the socket over which to send the timeout command
    user -- the user to be timed out
    secs -- the length of the timeout in seconds (default 600)
    """
    chat(sock, ".timeout {}".format(user, secs))

# ----- network functions -----
s = socket.socket()
s.connect((HOST, PORT))
s.send("PASS {} rn".format(PASS).encode("utf-8"))
s.send("NICK {} rn".format(NICK).encode("utf-8"))
s.send("JOIN {} rn".format(CHAN).encode("utf-8"))

# pattern we are looking for
CHAT_MSG=re.compile(r"^:w+!w+@w+.tmi.twitch.tv PRIVMSG #w+ :")
while True:
    response = s.recv(1024).decode("utf-8")
    if response == "PING :tmi.twitch.tvrn":
        s.send("PONG :tmi.twitch.tvrn".encode("utf-8"))
    else:
        username = re.search(r"w+", response).group(0) # return the entire match
        message = CHAT_MSG.sub("", response)
        print(username + ": " + message)
        for pattern in cfg.PATT:
            if re.match(pattern,message):
                timeout(s,username,10)
                break
    time.sleep(1/cfg.RATE)

字符串是一个抽象,代表Unicode codepoint的序列。要将字符串变成一个字节序列,您需要编码字符串,也就是决定要如何在电线上表示文本。对于抽搐,使用UTF-8:

full_msg = "PRIVMSG #{} :{}".format(cfg.CHAN, msg)
msg_encoded = full_msg.encode("utf-8")
sock.send(msg_encoded)

最新更新