简体   繁体   English

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

[英]python: IRC bot involunteer infinity loop

I've coded this python irc bot for twitch, and i have an issue: after an average of 20 minutes without messages to read, the bot send in the prompt an infinity of useless messages, and the bot can't work, even if there is messsages to read (and after a few hours, the hosting machine crash... because of the place that it takes in RAM). 我已经将这个python irc bot编码为抽搐,我有一个问题:在平均20分钟没有消息阅读之后,机器人在提示中发送无限的无用消息,并且机器人无法工作,即使有阅读的消息(几个小时后,主机崩溃...因为它占用RAM的地方)。 I putted here the code... If you know a way to transform the receiving line to an event or something else... 我在这里推出了代码...如果你知道一种将接收线转换为事件或其他东西的方法......

-*- 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)

Thanks for help. 感谢帮助。

我发现自己如何解决这个问题:在拆分线后, elif "PING" in recu: s.send("PONG :" + recu.split(":")[1])添加elif "PING" in recu: s.send("PONG :" + recu.split(":")[1])问题是机器人穿上从抽搐回应ping,所以抽搐踢他......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM