A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

67 wiersze
2.4 KiB

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