Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import re
  2. import os.path
  3. import html
  4. # twisted imports
  5. from twisted.words.protocols import irc
  6. from twisted.internet import reactor, protocol
  7. from twisted.python import log
  8.  
  9. # system imports
  10. import time, sys
  11.  
  12.  
  13. class MessageLogger:
  14. """
  15. An independent logger class (because separation of application
  16. and protocol logic is a good thing).
  17. """
  18. def __init__(self, file):
  19. self.file = file
  20.  
  21. def log(self, message):
  22. """Write a message to the file."""
  23. timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
  24. self.file.write('%s %s\n' % (timestamp, message))
  25. self.file.flush()
  26.  
  27. def close(self):
  28. self.file.close()
  29.  
  30. class LogBot(irc.IRCClient):
  31. """A logging IRC bot."""
  32.  
  33. nickname = "dank nick"
  34. password = "your oath"
  35.  
  36. count = 0
  37. prevTime = 0
  38.  
  39. def connectionMade(self):
  40. irc.IRCClient.connectionMade(self)
  41. self.logger = MessageLogger(open(self.factory.filename, "a"))
  42. self.logger.log("[connected at %s]" %
  43. time.asctime(time.localtime(time.time())))
  44.  
  45. def connectionLost(self, reason):
  46. irc.IRCClient.connectionLost(self, reason)
  47. self.logger.log("[disconnected at %s]" %
  48. time.asctime(time.localtime(time.time())))
  49. self.logger.close()
  50.  
  51.  
  52. # callbacks for events
  53.  
  54. def signedOn(self):
  55. """Called when bot has succesfully signed on to server."""
  56. self.join(self.factory.channel)
  57.  
  58. def joined(self, channel):
  59. """This will get called when the bot joins the channel."""
  60. self.logger.log("[I have joined %s]" % channel)
  61.  
  62. def privmsg(self, user, channel, msg):
  63. """This will get called when the bot receives a message."""
  64. user = user.split('!', 1)[0]
  65. self.logger.log("<%s> %s" % (user, msg))
  66.  
  67. if msg[0] == '!':
  68. print("command executed")
  69.  
  70. def action(self, user, channel, msg):
  71. """This will get called when the bot sees someone do an action."""
  72. user = user.split('!', 1)[0]
  73. self.logger.log("* %s %s" % (user, msg))
  74.  
  75. # irc callbacks
  76.  
  77. def irc_NICK(self, prefix, params):
  78. """Called when an IRC user changes their nickname."""
  79. old_nick = prefix.split('!')[0]
  80. new_nick = params[0]
  81. self.logger.log("%s is now known as %s" % (old_nick, new_nick))
  82.  
  83.  
  84. # For fun, override the method that determines how a nickname is changed on
  85. # collisions. The default method appends an underscore.
  86. def alterCollidedNick(self, nickname):
  87. """
  88. Generate an altered version of a nickname that caused a collision in an
  89. effort to create an unused related name for subsequent registration.
  90. """
  91. return nickname + '^'
  92.  
  93.  
  94.  
  95. class LogBotFactory(protocol.ClientFactory):
  96. """A factory for LogBots.
  97.  
  98. A new protocol instance will be created each time we connect to the server.
  99. """
  100.  
  101. def __init__(self, channel, filename):
  102. self.channel = channel
  103. self.filename = filename
  104.  
  105. def buildProtocol(self, addr):
  106. p = LogBot()
  107. p.factory = self
  108. return p
  109.  
  110. def clientConnectionLost(self, connector, reason):
  111. """If we get disconnected, reconnect to server."""
  112. connector.connect()
  113.  
  114. def clientConnectionFailed(self, connector, reason):
  115. print "connection failed:", reason
  116. reactor.stop()
  117.  
  118. if __name__ == '__main__':
  119. # initialize logging
  120. log.startLogging(sys.stdout)
  121.  
  122. # create factory protocol and application
  123. f = LogBotFactory(sys.argv[1], sys.argv[2])
  124.  
  125. # connect factory to this host and port
  126. reactor.connectTCP("irc.twitch.tv", 6667, f)
  127.  
  128. # run bot
  129. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement