Python/Twisted IRC 机器人日志记录问题
嘿。我最近才开始使用 Python,我的朋友建议使用 Twisted 作为创建这个 IRC 机器人的方法,因为它会简单得多。这是我到目前为止的代码(很大程度上基于 logbot.py 哈哈)。
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
from twisted.python import log
import sys, time
class MessageLogger:
def __init__(self, file):
self.file = file
def log(self, message):
timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
self.file.write('%s %s\n' % (timestamp, message))
self.file.flush()
def close(self):
self.file.close()
class IRCProtocol(irc.IRCClient):
nickname = "3n7rar3"
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger(open(self.factory.filename, "a"))
def signedOn(self):
print 'Success! Connection established.'
self.join(self.factory.channels)
print 'Joined channel', self.factory.channels
def privmsg(self, user, channel, msg):
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
class IRCFactory(protocol.ClientFactory):
protocol = IRCProtocol
channels = "#testing"
def __init__(self, channel, filename):
self.channel = channel
self.filename = filename
def clientConnectionFailed(self, connector, reason):
print "Connection failed because of %s" % reason
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost: %s" % reason
connector.connect()
if __name__ == "__main__":
log.startLogging(sys.stdout)
host, port = "irc.freenode.net", 6667
fact = IRCFactory(sys.argv[1],sys.argv[2])
reactor.connectTCP(host, port, fact)
reactor.run()
问题是机器人只记录通道消息,但我也希望它记录通道连接和部分。实现这一目标的最简单方法是什么?谢谢。
Hey. I only recently started using Python, and my friend suggested using Twisted as a means to create this IRC bot, as it will be a lot simpler. Here is the code I have so far (which is heavily based off logbot.py haha)
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
from twisted.python import log
import sys, time
class MessageLogger:
def __init__(self, file):
self.file = file
def log(self, message):
timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
self.file.write('%s %s\n' % (timestamp, message))
self.file.flush()
def close(self):
self.file.close()
class IRCProtocol(irc.IRCClient):
nickname = "3n7rar3"
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger(open(self.factory.filename, "a"))
def signedOn(self):
print 'Success! Connection established.'
self.join(self.factory.channels)
print 'Joined channel', self.factory.channels
def privmsg(self, user, channel, msg):
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
class IRCFactory(protocol.ClientFactory):
protocol = IRCProtocol
channels = "#testing"
def __init__(self, channel, filename):
self.channel = channel
self.filename = filename
def clientConnectionFailed(self, connector, reason):
print "Connection failed because of %s" % reason
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost: %s" % reason
connector.connect()
if __name__ == "__main__":
log.startLogging(sys.stdout)
host, port = "irc.freenode.net", 6667
fact = IRCFactory(sys.argv[1],sys.argv[2])
reactor.connectTCP(host, port, fact)
reactor.run()
The problem with this is that the bot only logs channel messages, but I would also like it to log channel joins and parts. What is the easiest way to accomplish this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义连接和部分的方法并在其中记录:
API文档和源代码可能对您将来有用。
Define methods for joins and parts and log inside them too:
The api documentation and source code are probably useful for you in the future.
文档对我来说似乎很清楚。重写
userJoined
和userLeft
方法,就像您对privmsg
所做的那样。The documentation seems pretty clear to me. Override the
userJoined
anduserLeft
methods like you did forprivmsg
.