IRC ボットをローカル サーバーに実装しようとしています。私が使用しているボットは、Eric Florenzano のブログにあるものと同じです。これは簡略化されたコードです(実行する必要があります)
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.
適切に動作している場合、コマンドの最初の引数として指定された適切なチャネルにログインする必要があります (たとえば、python module2.py botwar. は channel #botwar. になります)。チャネル内のいずれかが何かを送信すると、「こんにちは」と応答する必要があります。
サーバーでNGIRCを実行していますが、mIRC またはその他の IRC クライアントから接続すると動作します。
断続的に切断される理由について解決策を見つけることができません。理由についての助けをいただければ幸いです。ありがとうございました!