Advertisement
linuxgnuru

IRC bot

Aug 25th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. """
  2.  
  3. * It joins channels when invited
  4. * It replies to private messages
  5. * It (intermittently) speaks in channels when others are speaking
  6. * It also conveniently presents protocol information on stdout.
  7.  
  8. Requires the irc package, obtainable by typing
  9.  
  10.    pip install irc
  11.  
  12. """
  13.  
  14. import argparse
  15. import irc.client
  16. import irc.events
  17. import random
  18.  
  19. parser = argparse.ArgumentParser(description='Minetrek')
  20. parser.add_argument('--server', default='localhost',
  21.                     help='Server to connect to (defaults to localhost)')
  22. parser.add_argument('--port', type=int, default=6667,
  23.                     help='Port to connect to')
  24. parser.add_argument('--nick', default='minetrek_help',
  25.                     help='Nickname to connect with')
  26. parser.add_argument('--probability', type=int, default=5,
  27.                     help='Probability to reply in channel per message')
  28. parser.add_argument('--verbose', action='store_true',
  29.                     help='Print to stdout all events')
  30. parser.add_argument('--debug', action='store_true', help='Debug messages')
  31. options = parser.parse_args()
  32.  
  33. def debug(*args):
  34.     "Prints all arguments, if the debug option is enable."
  35.  
  36.     if options.debug:
  37.         for x in args:
  38.             print x,
  39.         print
  40.  
  41.  
  42. Random = random.Random()
  43. client = irc.client.IRC()
  44. server = client.server()
  45. channellist = []
  46.  
  47. # Default event processor
  48. def process_event(connection, event):
  49.     """
  50.    Prints the parameters of the supplied event: type, arguments, source, and target.
  51.    """
  52.     print "{0}: {1} ({2}->{3})".format(event.type, event.arguments, event.source,
  53.                                        event.target)
  54.  
  55. def pubmsg_handler(connection, event):
  56.     text = event.arguments[0].lower()
  57.  
  58.     if 'joined the Game' in text:
  59.             user = event.source.split('!')[0]
  60.             connection.privmsg(event.target, "Computer: " + user + ' To visit ships, type /nexus.  If you wish to build type /academy and read all the signs.')
  61.     else:
  62.         if 'Trekkie' in text:
  63.             if 'enterprise' in text or 'where is' in text or 'build' in text or 'promote' in text or 'rank' in text or 'self destruct' in text:
  64.                 if 'enterprise' in text or 'where is' in text:
  65.                     user = event.source.split('!')[0]
  66.                     connection.privmsg(event.target, "Computer: " + user + ', type /nexus')
  67.                 if 'build' in text or 'promote' in text or 'rank' in text:
  68.                     user = event.source.split('!')[0]
  69.                     connection.privmsg(event.target, "Computer: " + user + ', type /academy and read the signs')
  70.                 if 'self destruct' in text:
  71.                     user = event.source.split('!')[0]
  72.                     connection.privmsg(event.target, "Computer: " + user + ', Destruct sequence completed and engaged. Awaiting final code for one-minute countdown')
  73. #        else:
  74. #            if 'enterprise' in text or 'where is' in text or 'build' in text or 'promote' in text or 'rank' in text or 'self destruct' in text:
  75. #                if 'enterprise' in text or 'where is' in text:
  76. #                    user = event.source.split('!')[0]
  77. #                    connection.privmsg(event.target, "Computer: " + user + ', type /nexus')
  78. #                if 'build' in text or 'promote' in text or 'rank' in text:
  79. #                    user = event.source.split('!')[0]
  80. #                    connection.privmsg(event.target, "Computer: " + user + ', type /academy and read the signs')
  81. #                if 'self destruct' in text:
  82. #                    user = event.source.split('!')[0]
  83. #                    connection.privmsg(event.target, "Computer: " + user + ', Destruct sequence completed and engaged. Awaiting final code for one-minute countdown')
  84. #  else:
  85.  
  86. #     if 'computer:' in text:
  87. #         user = event.source.split('!')[0]
  88. #            connection.privmsg(event.target, "Computer: " + user + ', Unknown command.')
  89.  
  90. def invite_handler(connection, event):
  91.     """
  92.    Joins a channel when invited.
  93.    """
  94.     for i in event.arguments:
  95.         server.join(i)
  96.  
  97. if options.verbose:
  98.     debug('Registering default handler for all messages')
  99.     for v in irc.events.all:
  100.         client.add_global_handler(v, process_event)
  101. else:
  102.     debug('Registering default handler for protocol messages')
  103.     debug(irc.events.protocol)
  104.     for v in irc.events.protocol:
  105.         client.add_global_handler(v, process_event)
  106.  
  107. client.add_global_handler('pubmsg', pubmsg_handler)
  108. client.add_global_handler('privmsg', privmsg_handler)
  109. client.add_global_handler('invite', invite_handler)
  110. server.connect(options.server, options.port, options.nick)
  111. client.process_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement