【问题标题】:python: IRC bot involunteer infinity looppython: IRC bot involunteer 无限循环
【发布时间】: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)

感谢您的帮助。

【问题讨论】:

    标签: python bots irc twitch


    【解决方案1】:

    我找到了解决问题的方法:在分割线之后,添加elif "PING" in recu: s.send("PONG :" + recu.split(":")[1]) 问题是机器人不响应 twitch 的 ping,所以 twitch 踢了他...

    【讨论】:

      猜你喜欢
      • 2013-04-02
      • 2012-10-15
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多