2017-09-18 10 views
0

こんにちは私は友人のチャンネルのツイッチボットを作る方法を学んでいます。「str」ではなく「str」のようなオブジェクトが必要です。

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: :[email protected]_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' 
:ボットはすべてのボディは、単語を使用してタイムアウトするsuppousedよう は、今私はボットがチャットルームに参加するが、私は行政の一部を把握することができませんでし作ることができた ではなく、私はこのエラーを取得する「誓います」

CODE

#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 {} \r\n".format(PASS).encode("utf-8")) 
s.send("NICK {} \r\n".format(NICK).encode("utf-8")) 
s.send("JOIN {} \r\n".format(CHAN).encode("utf-8")) 


# pattern we are looking for 
CHAT_MSG=re.compile(r"^:\w+!\[email protected]\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") 

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) # 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) 
+0

可能な重複(https://stackoverflow.com/questions/21233340/sending-string-via-socket-python) –

答えて

1

ストリングは、Unicodeコードポイントのシーケンスを表す、抽象化です。 文字列を一連のバイトに変換するには、文字列をエンコードする必要があります。別名として、テキスト上にテキストをどのように表示するかを決める必要があります。単収縮のために、UTF-8を使用する:[ソケット(パイソン)を介して文字列を送信する]の

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

'socket.send'はバイトを必要としませんか?あなたはここに 'str'を与えていませんか? –

+0

'socket.send'はバイトを期待しています:https://docs.python.org/3/library/socket.html#socket.socket.send – Felk

+0

はい、最初の行でstrからbytesに移動します。しかし、2行目の形式の結果はstrではありませんか? –

関連する問題