A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

76 linhas
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. ## Imports
  3. import re, time
  4. from config.irc import *
  5. from config.secure import *
  6. from irc import command_handler
  7. from irc.connection import *
  8. from irc.data import Data
  9. connection = None
  10. def get_connection():
  11. connection = Connection(HOST, PORT, NICK, IDENT, REALNAME)
  12. return connection
  13. def startup(conn):
  14. global connection
  15. connection = conn
  16. command_handler.load_commands(connection)
  17. connection.connect()
  18. def main():
  19. read_buffer = str()
  20. while 1:
  21. try:
  22. read_buffer = read_buffer + connection.get()
  23. except BrokenSocketException:
  24. print "Socket has broken on front-end; restarting bot..."
  25. return
  26. lines = read_buffer.split("\n")
  27. read_buffer = lines.pop()
  28. for line in lines:
  29. line = line.strip().split()
  30. data = Data()
  31. data.line = line
  32. if line[1] == "JOIN":
  33. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  34. data.chan = line[2][1:]
  35. command_handler.check("join", data) # check if there's anything we can respond to, and if so, respond
  36. if line[1] == "PRIVMSG":
  37. data.nick, data.ident, data.host = re.findall(":(.*?)!(.*?)@(.*?)\Z", line[0])[0]
  38. data.msg = ' '.join(line[3:])[1:]
  39. data.chan = line[2]
  40. if data.chan == NICK: # this is a privmsg to us, so set 'chan' as the nick of the sender
  41. data.chan = data.nick
  42. command_handler.check("msg_private", data) # only respond if it's a private message
  43. else:
  44. command_handler.check("msg_public", data) # only respond if it's a public (channel) message
  45. command_handler.check("msg", data) # check for general messages
  46. if data.msg.startswith("!restart"): # hardcode the !restart command (we can't restart from within an ordinary command)
  47. if data.host in OWNERS:
  48. print "Restarting bot per owner request..."
  49. return
  50. if line[0] == "PING": # If we are pinged, pong back to the server
  51. connection.send("PONG %s" % line[1])
  52. if line[1] == "376":
  53. if NS_AUTH: # if we're supposed to auth to nickserv, do that
  54. connection.say("NickServ", "IDENTIFY %s %s" % (NS_USER, NS_PASS))
  55. time.sleep(3) # sleep for a bit so we don't join channels un-authed
  56. for chan in CHANS: # join all of our startup channels
  57. connection.join(chan)