replay.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ##NEEDS
  2. #adding a bold character for '<user> MEANT so say'
  3. from event import Event
  4. import requests
  5. import difflib
  6. import math
  7. class Replay:
  8. def __init__(self, events=None, printer_handle=None, bot=None, say=None):
  9. self.events = events
  10. self.printer = printer_handle
  11. self.interests = ['__.replay__']
  12. self.bot = bot
  13. self.say = say
  14. self.bot.mem_store['replace'] = {}
  15. #define a key for _recent since that will not be a potential channel name
  16. self.bot.mem_store['replace']['_recent'] = []
  17. replace = Event("__.r__")
  18. replace.define(msg_definition=".*")
  19. replace.subscribe(self)
  20. self.bot.register_event(replace, self)
  21. self.help = ".replay <number of lines | optional>"
  22. self.MAX_BUFFER_SIZE = 300
  23. self.MAX_HISTORY_SIZE = 10
  24. def get_replacement_message(self, channel=None, find_msg=''):
  25. """Looks through the mem_store to find the most recent message containing find_msg"""
  26. if not channel:
  27. print "couldnt find channel"
  28. return None
  29. #must have at least one msg to search for and channel to look it up in
  30. if len(find_msg) == 0 or not channel:
  31. print "find_msg is empty"
  32. return None
  33. #search for a matching string and saves the index of that entry.
  34. #Searches from most recent to oldest.
  35. found_index = -1
  36. for index, line in enumerate(self.bot.mem_store['replace'][channel]):
  37. message = line.decode('utf-8','ignore')
  38. msg_index = message.find(">")
  39. message = message[msg_index:]
  40. print line
  41. #if the current entry of mem_store contains our string, we set the index and then BREAK to stop looking
  42. if find_msg.decode('utf-8','ignore') in message:
  43. found_index = index
  44. break
  45. #check to see if index values are positive. if not, string was not found and we're done
  46. if found_index == -1 :
  47. print "couldnt find a good match"
  48. return None
  49. #returns the entire line
  50. submission = self.bot.mem_store['replace'][channel][found_index]
  51. return submission
  52. def is_number(self, e):
  53. try:
  54. int(e)
  55. return True
  56. except ValueError:
  57. return False
  58. def handle(self, event):
  59. #first we see if we're going to try a replace or just add a line to the mem_store
  60. if event.msg.startswith(".replay "):
  61. msg = event.msg.replace('.replay ', '')
  62. user = event.user
  63. msglist = self.bot.mem_store['replace'][event.channel]
  64. l = len(msglist)
  65. if self.is_number(msg):
  66. msg = int(msg)
  67. if msg > l:
  68. msg = l - 1
  69. self.printer("PRIVMSG " + event.channel + ' :' + user + ", I'm sending you a message with the last " + `msg` + ' recorded messages. \n')
  70. else:
  71. self.printer("PRIVMSG " + event.channel + ' :Invalid argument \n')
  72. x = msg
  73. while x >= 1:
  74. self.printer("PRIVMSG " + user + ' :' + msglist[x] + '\n')
  75. x = x - 1
  76. #msg_index = newString.find(">")
  77. #message = newString[msg_index + 2:]
  78. ##message = message.replay(find_msg, replace_msg)
  79. #user = newString[1:msg_index]
  80. #pybot sends the new replacement message to the chat
  81. #self.printer("PRIVMSG " + event.channel + ' :' + user + " MEANT to say: " + message + '\n')