正在尝试向 Twitch Chat 发送消息
Trying to send Message TO Twitch Chat
我已经将机器人设置为对某些命令做出反应,例如使用文字朗读和在聊天使用命令时播放 mp3 剪辑,但是,我不知道如何让它到达例如,当有人说 "Hello Fuzzy" 时,通过发送回聊天消息来响应。我收到 TypeError:需要一个类似字节的对象,而不是 'str'。我显然做错了什么,但我检查过的其他机器人都以不同的方式编码,并且尽量不必重新做整个事情。感谢您提供的任何帮助!!!
# Config portion
import socket
import time
import re
import pyttsx3
import soundfx
HOST = "irc.chat.twitch.tv" # the twitch irc server
PORT = 6667 # always use port 6667
NICK = "fuzzybottgaming" # twitch username, lowercase
PASS = "oauth:[]" # your twitch OAuth token
CHAN = "#[my channel]" # the channel you want to join
# Message Rate
RATE = (20 / 30) # messages per second
#BANN HAMMER
PATT = [
r"swear",
# ...
r"some_pattern"
]
# bot.py portion
# Network functions go here
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"))
def chat(sock, msg):
'''
Send a chat message to the server.
sock -- the socket over which to send the message
msg -- the message to be sent
'''
sock.send("PRIVMSG #{} :{}".format(CHAN, msg))
def ban(sock, user):
'''
Ban a user from the current channel.
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=10):
'''
Time out a user for a set period of time
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))
# Make sure you prefix the quotes with an 'r'
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
while True:
response = s.recv(1024).decode("utf-8")
print(response)
if response == "PING :tmi.twitch.tv\r\n":
s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
print("Pong")
else:
username = re.search(r"\w+", response).group(0) # return the entire
match
message = CHAT_MSG.sub("", response)
print(username + ": " + message)
if 'hello fuzzy' in message:
s.send("Hello to you too " + username).encode("utf-8")
#TTS and MP3 sound effects section taken out#
for pattern in PATT:
if re.match(pattern,message):
ban(s, username)
break
time.sleep(1 / RATE)
我知道我错在哪里了。我有一个额外的 '#' 给 CHAN,在 def chat 中我需要添加 '\r\n' 和 '.encode("utf-8")'
我已经将机器人设置为对某些命令做出反应,例如使用文字朗读和在聊天使用命令时播放 mp3 剪辑,但是,我不知道如何让它到达例如,当有人说 "Hello Fuzzy" 时,通过发送回聊天消息来响应。我收到 TypeError:需要一个类似字节的对象,而不是 'str'。我显然做错了什么,但我检查过的其他机器人都以不同的方式编码,并且尽量不必重新做整个事情。感谢您提供的任何帮助!!!
# Config portion
import socket
import time
import re
import pyttsx3
import soundfx
HOST = "irc.chat.twitch.tv" # the twitch irc server
PORT = 6667 # always use port 6667
NICK = "fuzzybottgaming" # twitch username, lowercase
PASS = "oauth:[]" # your twitch OAuth token
CHAN = "#[my channel]" # the channel you want to join
# Message Rate
RATE = (20 / 30) # messages per second
#BANN HAMMER
PATT = [
r"swear",
# ...
r"some_pattern"
]
# bot.py portion
# Network functions go here
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"))
def chat(sock, msg):
'''
Send a chat message to the server.
sock -- the socket over which to send the message
msg -- the message to be sent
'''
sock.send("PRIVMSG #{} :{}".format(CHAN, msg))
def ban(sock, user):
'''
Ban a user from the current channel.
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=10):
'''
Time out a user for a set period of time
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))
# Make sure you prefix the quotes with an 'r'
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
while True:
response = s.recv(1024).decode("utf-8")
print(response)
if response == "PING :tmi.twitch.tv\r\n":
s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
print("Pong")
else:
username = re.search(r"\w+", response).group(0) # return the entire
match
message = CHAT_MSG.sub("", response)
print(username + ": " + message)
if 'hello fuzzy' in message:
s.send("Hello to you too " + username).encode("utf-8")
#TTS and MP3 sound effects section taken out#
for pattern in PATT:
if re.match(pattern,message):
ban(s, username)
break
time.sleep(1 / RATE)
我知道我错在哪里了。我有一个额外的 '#' 给 CHAN,在 def chat 中我需要添加 '\r\n' 和 '.encode("utf-8")'