Twitch 机器人使用 Python 3
Twitch Bot using Python 3
我已经搜索了很多地方,包括 Twitch Developers 网站,但到目前为止还没有关于如何修复此错误的答案。 (是的,这是使用 Python 2.x 编写的,我正在使用 3.x,但我认为任何错误都可能很容易自己找出来......不)
我在聊天中收到 TwitchBot\utils.py",第 17 行
sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg))
类型错误:需要一个类似字节的对象,而不是 '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()
IDK 如果此时这对您有用,但您没有在第 17 行以 UTF-8 编码您的消息,这是给您错误的原因。
我已经搜索了很多地方,包括 Twitch Developers 网站,但到目前为止还没有关于如何修复此错误的答案。 (是的,这是使用 Python 2.x 编写的,我正在使用 3.x,但我认为任何错误都可能很容易自己找出来......不)
我在聊天中收到 TwitchBot\utils.py",第 17 行 sock.send("PRIVMSG #{} :{}\r\n".format(cfg.CHAN, msg)) 类型错误:需要一个类似字节的对象,而不是 '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()
IDK 如果此时这对您有用,但您没有在第 17 行以 UTF-8 编码您的消息,这是给您错误的原因。