Python 每次有人使用表情符号时,Twitch机器人就会崩溃,如:flush:in chat
我创建了一个twitch聊天机器人并添加了一些简单的命令,但是每次有人使用表情符号时,比如Python 每次有人使用表情符号时,Twitch机器人就会崩溃,如:flush:in chat,python,crash,encode,bots,twitch,Python,Crash,Encode,Bots,Twitch,我创建了一个twitch聊天机器人并添加了一些简单的命令,但是每次有人使用表情符号时,比如:flush:或:speak\u no\u evil:这个机器人就会崩溃。我猜它把它理解为一个词,因此崩溃了 我的主要代码如下: import cfg import utils import socket import re import time, thread from time import sleep import os def main(): s = socket.socket()
:flush:
或:speak\u no\u evil:
这个机器人就会崩溃。我猜它把它理解为一个词,因此崩溃了
我的主要代码如下:
import cfg
import utils
import socket
import re
import time, thread
from time import sleep
import os
def main():
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, "HeyGuys")
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)
if message.strip() == "!time":
utils.chat(s, "It currently " + time.strftime("%I:%M %p %Z"))
if "Kappa" in message and not "PogChamp" in message:
utils.chat(s, "Kappa")
if "PogChamp" in message and not "Kappa" in message:
utils.chat(s, "PogChamp")
if message.strip() == "!kb":
utils.chat(s, "Razer Blackwidow Tournament Edition.")
if message.strip() == "!skin":
utils.chat(s, "https://puu.sh/sjE7I/58820f8c10.osk")
if message.strip() == "!tablet":
utils.chat(s, "Wacom CTH490AK")
if message.strip() == "!area":
utils.chat(s, "https://puu.sh/sfr9E/1abc132c23.png")
if message.strip() == "!oldskin":
utils.chat(s, "https://puu.sh/sjEOD/5923cfabc0.osk")
if message.strip() == "!profile":
utils.chat(s, "https://osu.ppy.sh/u/3892208")
if message.strip() == "!np":
f = open("C:/Users/Grellheist/Desktop/osu stream/Files/np_playing_DL.txt", "r")
if(os.stat("C:/Users/Grellheist/Desktop/osu stream/Files/np_playing_DL.txt").st_size == 0):
utils.chat(s, "There is no map being played right now!")
else:
f = open("C:/Users/Grellheist/Desktop/osu stream/Files/np_playing_DL.txt", "r")
utils.chat(s, f.read())
if message.strip() == "!bot":
utils.chat(s, "I was created by Grellheist and was coded using Python 3.")
sleep(1)
if __name__ == "__main__":
main()
如何让机器人忽略这些表情符号 你可以试着过滤掉那些字符:你可以把你的
.encode()
调用包装在一个try/except块中,如果你得到一个UnicodeEncodeError
异常,就跳过这个消息。你试着用python 3运行它吗?你甚至没有想到,try/except解决方案奏效了。非常感谢你!你可以试着过滤掉那些字符:你可以把你的.encode()
调用包装在一个try/except块中,如果你得到一个UnicodeEncodeError
异常,就跳过这个消息。你试着用python 3运行它吗?你甚至没有想到,try/except解决方案奏效了。非常感谢你!