python:IRC机器人志愿者无限循环

时间:2017-01-21 12:33:18

标签: python bots irc twitch

我已经将这个python irc bot编码为抽搐,我有一个问题: 在没有消息阅读的平均20分钟之后,机器人在提示中发送无限的无用消息,并且机器人无法工作,即使有一些消息要读(并且几个小时之后,主机也是如此)崩溃...因为它占用RAM的地方)。 我在这里推出了代码......如果你知道将接收线转换为事件或其他东西的方法......

-*- coding: utf-8 -*-
import socket
import sys
import time

CHANNEL = "#twitch" #actually I tested my bot on an other channel, where I've the admins rights
s = socket.socket()

def connection():
    print("connecting...")
    s.connect(("irc.chat.twitch.tv", 6667))
    print("identifing...")
    s.send("PASS " + "oauth:*****************************" + "\r\n") #i censured for evident security reason
    s.send("NICK " + "mistercraft" + "\r\n")
    print("joining channel " + CHANNEL)
    s.send("JOIN " + CHANNEL + "\r\n")
    print("Connected")

def send(Message):
    s.send("PRIVMSG "+CHANNEL+" :"+ Message + "\r\n")
    print("Sent : " + Message)

connection()
send("Hello world !!!")

while 1:

    text = ""
    recu = s.recv(2040) #receive line, where there is the issue
    if len(recu.split(":")) >= 3:
        user = recu.split("!")[0]
        user = user.split(":")[1]
        for i in range(2, len(recu.split(":")), 1):
            text = text + recu.split(":")[i] + ":"
        print(user+" : "+text)

    #Code here (like 'if' loops)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我发现自己如何解决这个问题:在分裂线之后,添加elif "PING" in recu: s.send("PONG :" + recu.split(":")[1])问题是机器人没有对抽搐做出反应,所以抽搐踢了他......