IRC Twitch以某种方式开始忽略消息

时间:2019-05-23 03:45:38

标签: python python-3.x

我只是想制作一个“ Twitch Plays”游戏。我使用了制作Twitch Plays游戏的教程。我需要每五分钟重新运行一次此代码,因为它会忽略消息。我正在Scratch上进行Threes游戏。我检查了所有可能的错误,并且效果很好。这款游戏是基于包含数字的移动磁贴。关于IRC抽搐忽略所有消息的任何建议将受到高度赞赏。

代码如下所示。

import socket
import pyautogui
import time

SERVER = "irc.twitch.tv"
PORT = 6667
PASS = "oauth:fz5kp09su0lz46vnos78sklvpjnu6l"
BOT = "TwitchBot"
CHANNEL = "olejika2016"
OWNER   = "olejika2016"
message = ""
irc = socket.socket()
irc.connect((SERVER,PORT))
irc.send((  "PASS "+PASS+"\n"+
            "NICK "+BOT+"\n"+
            "JOIN #"+CHANNEL+"\n").encode())



def twitch():
    def joinchat():
        loading = True
        while loading:
            readbuffer_join = irc.recv(1024)
            readbuffer_join = readbuffer_join.decode()
            for line in readbuffer_join.split("\n")[0:-1]:
                # print(line)
                loading = loadingComplete(line)
    def loadingComplete(line):
        if ("End of /NAMES list" in line):
            print("Bot has joined "+CHANNEL+"'s channel")
            sendMessage(irc, "Chat Room Joined")
            return False
        else:
            return True
    def sendMessage(irc, message):
        messageTemp = "PRIVMSG #" + CHANNEL + " :" + message
        irc.send((messageTemp+"\n").encode())
    def getUser(line):
        separate = line.split(":", 2)
        user = separate[1].split("!",1)[0]
        return user
    def getMessage(line):
        global message
        try:
            message = (line.split(":",2))[2]
        except:
            message = ""
        return message

    joinchat()

    while True:
        try:
            readbuffer = irc.recv(1024).decode()
        except:
            readbuffer = ""
        for line in readbuffer.split("\r\n"):
            if line == "":
                continue
            else:
                # print("2;"+line)
                user = getUser(line)
                message = getMessage(line)
                print(user+":"+message)
                def writeUser():
                    time.sleep(0.1)
                    pyautogui.typewrite(user)
                    pyautogui.press('enter')
                if message.lower() == "w":
                    pyautogui.press('up')
                    writeUser()
                if message.lower() == "s":
                    pyautogui.press('down')
                    writeUser()
                if message.lower() == "a":
                    pyautogui.press('left')
                    writeUser()
                if message.lower() == "d":
                    pyautogui.press('right')
                    writeUser()
                else:
                    pass
                user = ''
                message = ''

twitch()

1 个答案:

答案 0 :(得分:0)

for line in readbuffer.split("\r\n"):
            if line == "":
                continue
            elif "PING" is line and Console(line):
                msgg = "PONG tml.twitch.tv\r\n".encode()
                irc.send(msgg)
                print(msgg)
                continue
            else:
                user = getUser(line)
                message = getMessage(line)
                print(user+":"+message)