Search code examples
pythonbotsirctwitch

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


Solution

  • I found myself how to stop the issue : after the splitting lines, add elif "PING" in recu: s.send("PONG :" + recu.split(":")[1]) the issue was that the bot don't respond to the ping from twitch, so twitch kicked him...