【发布时间】:2023-03-25 02:04:02
【问题描述】:
我已经为 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)
感谢您的帮助。
【问题讨论】: