1

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 クライアントから接続すると動作します。

断続的に切断される理由について解決策を見つけることができません。理由についての助けをいただければ幸いです。ありがとうございました!

4

1 に答える 1

2

ボットがサーバーに接続したときにサーバーによって生成されたエラー出力が表示されることを確認することをお勧めします。私の勘では、問題は認証に関係している、またはngircがによって使用されるログイン/認証コマンドの1つを処理する方法に予期しない違いがある可能性がありますIRCClient

ほとんどの場合に適用される1つのアプローチは、トラフィックログをキャプチャすることです。tcpdumpやwiresharkなどのツールを使用します。

試すことができるもう1つのアプローチは、Twistedアプリケーション自体の内部でロギングを有効にすることです。これに使用twisted.protocols.policies.TrafficLoggingFactoryします:

from twisted.protocols.policies import TrafficLoggingFactory
appFactory = MomBotFactory(...)
logFactory = TrafficLoggingFactory(appFactory, "irc-")
reactor.connectTCP(..., logFactory)

これにより、「irc-」で始まるファイル(接続ごとに異なるファイル)に出力が記録されます。

また、いくつかのレベルのいずれかで、プロトコル実装に直接フックすることもできます。たとえば、受信したすべてのバイトを表示するには、次のようにします。

class MomBot(irc.IRCClient):
    def dataReceived(self, bytes):
        print "Got", repr(bytes)
        # Make sure to up-call - otherwise all of the IRC logic is disabled!
        return irc.IRCClient.dataReceived(self, bytes)

これらのアプローチの1つを実行すると、次のようなものが表示されることを願っています。

:irc.example.net 451 * :Connection not registered

これは意味すると思います...あなたは認証する必要がありますか?他に何かが表示された場合でも、これにより、接続が閉じられた正確な原因をより詳細に絞り込むことができれば幸いです。

tcpdumpまた、またはを使用wiresharkして、ngircと動作中のIRCクライアントの1つ(mIRCなど)間のトラフィックログをキャプチャし、2つのログを比較することもできます。mIRCが送信するさまざまなコマンドが何であれ、ボットにどのような変更を加える必要があるかを明確にする必要があります。

于 2012-12-04T16:31:53.020 に答える