Beispiel #1
0
class BasicBot(irc.IRCClient):
    """A basic IRC bot.

    Simply connects to the channel specified by its factory using the nick
    specified by the factory, logging connection information to the file
    specified by the factory.
    """

    def connectionMade(self):
        irc.IRCClient.connectionMade(self)
        self.setNick(self.factory.nickname)
        self.logger = MessageLogger(open(self.factory.filename, "a"))
        self.logger.log("[connected at %s]" % 
                        time.asctime(time.localtime(time.time())))

    def connectionLost(self, reason):
        irc.IRCClient.connectionLost(self, reason)
        self.logger.log("[disconnected at %s]" % 
                        time.asctime(time.localtime(time.time())))
        self.logger.close()

    def signedOn(self):
        self.join(self.factory.channel)
class LogBot(irc.IRCClient):
    """A logging IRC bot."""
    def connectionMade(self):
        irc.IRCClient.connectionMade(self)
        self.logger = MessageLogger(open(self.factory.filename, "a"))
        self.logger.log("[connected at %s]" %
                        time.asctime(time.localtime(time.time())))

    def connectionLost(self, reason):
        irc.IRCClient.connectionLost(self, reason)
        self.logger.log("[disconnected at %s]" %
                        time.asctime(time.localtime(time.time())))
        self.logger.close()


    # callbacks for events

    def signedOn(self):
        """Called when bot has succesfully signed on to server."""
        self.join(self.factory.channel)

    def joined(self, channel):
        """This will get called when the bot joins the channel."""
        self.logger.log("[I have joined %s]" % channel)

    def kickedFrom(self, channel, kicker, message):
        """This function is called, when bot is kicked out of the channel"""
        """Bot logs information and reconnects to the channel"""
        self.logger.log("[%s kicked me from %s - reason %s]" % (kicker, channel, message))
        self.join(self.factory.channel)

    def topicUpdated(self, user, channel, newTopic):
        """This function logs new topic to log"""
        self.logger.log("[%s changed topic on %s to %s]" % (user,channel,newTopic))

    def userKicked(self, kickee, channel, kicker, message):
        """When one of the users get kicked..."""
        self.logger.log("[%s has been kicked from %s by %s with message: %s]" % (kickee,channel,kicker,message))

    def privmsg(self, user, channel, msg):
        """This will get called when the bot receives a message."""
        user = user.split('!', 1)[0]
        self.logger.log("<%s> %s" % (user, msg))

        # Check to see if they're sending me a private message
        if channel == self.factory.nick:
            msg = "It isn't nice to whisper!  Play nice with the group."
            self.msg(user, msg)
            return

        # Otherwise check to see if it is a message directed at me
        if msg.startswith(self.factory.nick + ":") or self.factory.nick in msg:
            msg = "%s: I am a log bot by https://github.com/thelehhman" % user
            self.msg(channel, msg)
            self.logger.log("<%s> %s" % (self.factory.nick, msg))

    def action(self, user, channel, msg):
        """This will get called when the bot sees someone do an action."""
        user = user.split('!', 1)[0]
        self.logger.log("* %s %s" % (user, msg))

    # irc callbacks

    def irc_NICK(self, prefix, params):
        """Called when an IRC user changes their nickname."""
        old_nick = prefix.split('!')[0]
        new_nick = params[0]
        self.logger.log("%s is now known as %s" % (old_nick, new_nick))


    # For fun, override the method that determines how a nickname is changed on
    # collisions. The default method appends an underscore.
    def alterCollidedNick(self, nickname):
        """
        Generate an altered version of a nickname that caused a collision in an
        effort to create an unused related name for subsequent registration.
        """
        nickname = nickname+"_"
        return nickname