コード例 #1
0
ファイル: bot.py プロジェクト: ClareCat/CatBot
 def __init__(self, nickname, password):
     self.nickname = nickname
     self.password = password
     self.seen = Seen()
コード例 #2
0
ファイル: bot.py プロジェクト: ClareCat/CatBot
class CatBot(irc.IRCClient):

    def __init__(self, nickname, password):
        self.nickname = nickname
        self.password = password
        self.seen = Seen()
    
    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)

    def connectionMade(self):
        irc.IRCClient.connectionMade(self)

    def connectionLost(self, reason):
        irc.IRCClient.connectionLost(self, reason)     

    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.setNick("CatBot")
        self.seen.load_dict()

    def privmsg(self, user, channel, msg):
        """This will get called when the bot receives a message."""
        self.seen.update_dict(user, msg, str(datetime.datetime.utcnow()))
        self.seen.store_dict()
        user = user.split('!', 1)[0]

        #Check to see if they're executing the user command
        if msg.startswith("!"):
            self.parseFunctions(user, channel, msg)

        # Check to see if they're sending me a private message
        elif channel == self.nickname:
            msg = "Go away"
            self.msg(user, msg)

        # Otherwise check to see if it is a message directed at me
        elif msg.startswith(self.nickname + ":"):
            msg = "Meow {usr}".format(usr=user)
            self.msg(channel, msg)

    def parseFunctions(self, user, channel, msg):
        """Executes all of the function commands"""

        if msg.startswith("!user"):
            msg = functions.get_user(msg)

        elif msg.startswith("!weather"):
            msg = functions.weather(msg)

        elif msg.startswith("!jerkcity"):
            msg = functions.jerkcity()

        elif msg.startswith("!seen"):
            msg = self.seen.get_seen(msg)

        else:
            msg = "Invalid Request"
        self.msg(channel, msg)

    def action(self, user, channel, msg):
        """This will get called when the bot sees someone do an action."""
        self.seen.update_dict(user, msg, str(datetime.datetime.utcnow()))
        user = user.split('!', 1)[0]

    def irc_NICK(self, prefix, params):
        """Called when an IRC user changes their nickname."""
        old_nick = prefix.split('!')[0]
        new_nick = params[0]

    # 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.
        """
        return nickname + '^'