python:IRC 机器人无意识无限循环
python: IRC bot involunteer infinity loop
我已经为 twitch 编写了这个 python irc 机器人,但我有一个问题:
平均 20 分钟没有消息可读后,bot 会在提示中发送无穷无尽的无用消息,bot 无法工作,即使有消息可读(几个小时后,主机崩溃。 .. 因为它在 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)
感谢您的帮助。
我发现自己如何解决这个问题:在分割线之后,添加 elif "PING" in recu:
s.send("PONG :" + recu.split(":")[1])
问题是机器人不响应来自 twitch 的 ping,所以 twitch 踢了他...
我已经为 twitch 编写了这个 python irc 机器人,但我有一个问题: 平均 20 分钟没有消息可读后,bot 会在提示中发送无穷无尽的无用消息,bot 无法工作,即使有消息可读(几个小时后,主机崩溃。 .. 因为它在 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)
感谢您的帮助。
我发现自己如何解决这个问题:在分割线之后,添加 elif "PING" in recu:
s.send("PONG :" + recu.split(":")[1])
问题是机器人不响应来自 twitch 的 ping,所以 twitch 踢了他...