线程在irc聊天机器人

时间:2016-03-16 10:55:11

标签: python multithreading chat bots

编写一个我认为需要超过1个线程的聊天机器人。 First Main线程不断读取聊天内容。我不时需要检查我需要从URL((JSON)获得的版主并提供给主线程,以便他可以例如禁止某人而不是主持人。

第一个问题:如何每30秒运行一次线程。我不想暂停它。我想运行它,在获得主持人列表后完成,然后在一段时间后再次运行它。

主文件代码:

#Requests module, which gets JSON data from an url
#To get moderators in the chat.
def secondThread():
    while(True):
        getModsList()


def chatReading():
    #Creates socket to the twitch irc chat
    s = openSocket()
   #Joins a specific chat room
    joinRoom(s)

    while(True):
        #Revieves messages from irc                  
        derp = s.recv(8192)
        derp=derp.decode('utf-8')
        temp = str.split(derp, "\n")
        readbuffer = temp.pop()

        #Loop which prints scanned thing from twitch irc chat
        for line in temp:
            #Time stamp    
            print(time.strftime('%X'), end=" : ")
            #Pings server back    
            pingPong(s, line)

            #This just displays the message recieved more readable User:Message
            try:
                print(getUser(line) + " typed :" + getMessage(line))
            except (IndexError, UnicodeEncodeError):
                print("Not an user")


def main():

    thread_1 = threading.Thread(target=chatReading, args=[])
    thread_2 = threading.Thread(target=secondThread, args=[])
    thread_1.start()
    thread_2.start()

if __name__ == '__main__':
    main()

使用getModList的第二个文件

moderators = None

def getModsList():

    gotList = False

    while(gotList != True):
        jsonList = requests.get(CHANNEL_GROUP)
        #If the jsonList status_code == 200 that means that it got the list,
        #every other number is bad.
        statusFlag=jsonList.status_code

        if(statusFlag == 200):
            gotList = True
        else:
            print("Failed to get mods list, trying again")
            continue

        jsonList=jsonList.json()

        #this gets the mods list
        moderators = jsonList['chatters']['moderators']
        time.sleep(10)

第二个问题更多的是建议。我该如何规划项目?这是我的第一个更严肃的项目,我很难看到如何规划它。我想稍后为机器人添加更多功能,例如用户命令,mods,抽奖活动,其中用户按命令只能加入一次,5分钟后选择获胜者。提前谢谢。

0 个答案:

没有答案
相关问题