Created
May 3, 2010 03:09
-
-
Save yono/387703 to your computer and use it in GitHub Desktop.
twistedのサンプルを基にyonobotのIRC版書いた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# twisted imports | |
from twisted.words.protocols import irc | |
from twisted.internet import reactor, protocol | |
# system imports | |
import time, sys | |
from yonobot import yonobot | |
class LogBot(irc.IRCClient): | |
nickname = "yonobot" | |
calledtime = 0 | |
bot = yonobot.YonoBot() | |
def connectionMade(self): | |
irc.IRCClient.connectionMade(self) | |
def connectionLost(self, reason): | |
irc.IRCClient.connectionLost(self, reason) | |
# 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.""" | |
def privmsg(self, user, channel, msg): | |
"""This will get called when the bot receives a message.""" | |
user = user.split('!', 1)[0] | |
# Check to see if they're sending me a private message | |
if msg == self.nickname or msg == "よのぼっと": | |
msg = "はい" | |
self.notice(channel, msg) | |
self.calledtime = int(time.time()) | |
return | |
if (int(time.time()) - self.calledtime) < 60: | |
msg = self.bot.m.make_sentence('yono').encode('utf-8') | |
self.notice(channel, msg) | |
return | |
def action(self, user, channel, msg): | |
"""This will get called when the bot sees someone do an action.""" | |
user = user.split('!', 1)[0] | |
print msg, user | |
# 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] | |
# 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 + '_' | |
class LogBotFactory(protocol.ClientFactory): | |
"""A factory for LogBots. | |
A new protocol instance will be created each time we connect to the server. | |
""" | |
# the class of the protocol to build when new connection is made | |
protocol = LogBot | |
def __init__(self, channel): | |
self.channel = channel | |
def clientConnectionLost(self, connector, reason): | |
"""If we get disconnected, reconnect to server.""" | |
connector.connect() | |
def clientConnectionFailed(self, connector, reason): | |
print "connection failed:", reason | |
reactor.stop() | |
if __name__ == '__main__': | |
# create factory protocol and application | |
f = LogBotFactory(sys.argv[1]) | |
# connect factory to this host and port | |
reactor.connectTCP("chat.freenode.net", 6667, f) | |
# run bot | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment