我正試圖在本地服務器上實現IRC Bot。我正在使用的機器人與在Eric Florenzano's Blog找到的機器人相同。這是簡化的代碼(其應該運行)扭曲的IRC Bot連接重複丟失到本地主機
import sys
import re
from twisted.internet import reactor
from twisted.words.protocols import irc
from twisted.internet import protocol
class MomBot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
print "attempting to sign on"
self.join(self.factory.channel)
print "Signed on as %s." % (self.nickname,)
def joined(self, channel):
print "attempting to join"
print "Joined %s." % (channel,)
def privmsg(self, user, channel, msg):
if not user:
return
if self.nickname in msg:
msg = re.compile(self.nickname + "[:,]* ?", re.I).sub('', msg)
prefix = "%s: " % (user.split('!', 1)[0],)
else:
prefix = ''
self.msg(self.factory.channel, prefix + "hello there")
class MomBotFactory(protocol.ClientFactory):
protocol = MomBot
def __init__(self, channel, nickname='YourMomDotCom', chain_length=3,
chattiness=1.0, max_words=10000):
self.channel = channel
self.nickname = nickname
self.chain_length = chain_length
self.chattiness = chattiness
self.max_words = max_words
def startedConnecting(self, connector):
print "started connecting on {0}:{1}"
.format(str(connector.host),str(connector.port))
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s), reconnecting." % (reason,)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
if __name__ == "__main__":
chan = sys.argv[1]
reactor.connectTCP("localhost", 6667, MomBotFactory('#' + chan,
'YourMomDotCom', 2, chattiness=0.05))
reactor.run()
我在客戶工廠,它被到達並打印出正確的地址添加startedConnection方法:主機。然後它斷開並進入clientConnectionLost並打印錯誤:
Lost connection ([Failure instance: Traceback (failure with no frames):
<class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]), reconnecting.
如果正常工作應該登錄到適當的信道,指定爲在命令中第一個參數(例如蟒module2.py botwar將信道#。 botwar)。如果頻道中的任何人發送任何內容,它應該以「你好,」迴應。
我有NGIRC在服務器上運行,它工作,如果我從mIRC或任何其他IRC客戶端連接。
我無法找到一個解決方案,爲什麼它不斷斷開連接。任何幫助爲什麼將不勝感激。謝謝!
您正在運行哪臺IRC服務器?它是開源的嗎? – fmoo
*有*是因爲它斷開連接的原因,否則它不會斷開連接。然而,沒有足夠的信息讓任何人猜測可能的原因。請參閱http://sscce.org/和http://www.catb.org/esr/faqs/smart-questions.html –
@ Jean-PaulCalderone - 感謝您訪問這些網站。我添加了更多信息。它不屬於「短」的要求,但我不知道如何以任何其他方式做到這一點。希望這是一個更好的措辭問題。 – RyGuy