Commits

Andi Miller committed fa2bc38 Merge

Merged in afal/twistedbot (pull request #9)

Comments (0)

Files changed (6)

    tbot.msg(channel,"<3 "+user)
 love.rule='<3 TwistedBot'
 
+def thanks_ants(tbot, user, channel, msg):
+    name = msg.replace("!thanks ", '').strip()
+    thname = re.sub(r'^[bcdfghjklmnpqrstvwxyz]*', '', name.lower()) or name.lower()
+    tbot.msg(channel, "Thanks %s... Th%s" % (name, thname))
+thanks_ants.rule = '!thanks '
+
 def xory(question):
    return random.choice(re.split(",|or", question)).strip()
 
     message = msg.replace("!g", "", 1).strip()
     result = first_result(message)
     tbot.msg(channel, "%s: %s" % (user, result.encode("utf-8")))
-g.rule = '^!g'
+g.rule = '^!g '
 
 def gc(tbot, user, channel, msg):
     message = msg.replace("!gc", "", 1).strip()
-    result = first_result(message)
+    result = count_results(message)
     tbot.msg(channel, "%s: %s" % (user, result.encode("utf-8")))
-gc.rule = '^!gc'
+gc.rule = '^!gc '
 
 def gfight(tbot, user, channel, msg):
     regex = re.compile('TwistedBot: (.*?) vs (.*)')
 
 def gettweet(url):
     print "gettitle called with: " + url
-    tweet_id =  url.split('/')[-1] # Probably needs a better way to get the tweet id than this
+    tweet_id =  re.match(tweetauto.rule, url).groups()[-1]
     api_url = "https://api.twitter.com/1/statuses/show/%s.json" % tweet_id
     f = urllib.urlopen(api_url)
     data = f.read()
     text=gettweet(url)
     text=unescape(text)
     tbot.msg(channel,text.encode("utf-8"))
-tweetauto.rule = r'^https?://twitter.com/'
+tweetauto.rule = r'^https?://(www.)?twitter.com/\w+/status/([0-9]+)'
 
 
 if __name__ == '__main__':
 import re
 
 
+class Logger():
+    def log(self, *args):
+        pass
+
+
 class TestedBot:
     bot_messages = []
+    logger = Logger()
     functions = []
     messages = {}
+    __funcs = {}
 
     def __init__(self):
         pass
     def msg(self, channel, message):
         self.bot_messages.append((channel, message))
 
-    def register(self, func):
+    def register(self, func, name=None):
         self.functions.append(func)
+        if name:
+            self.__funcs[name] = func.rule
+
+    def rules(self):
+        messages = ["The rules and functions are as follows:"]
+        for func in self.__funcs:
+            messages.append("    %s = %s" % (func, self.__funcs[func]))
+        return messages
 
     def last_message(self):
         if len(self.bot_messages):
         for func in self.functions:
             if re.match(func.rule, message):
                 func(self, usr, channel, message)
+        return self.bot_messages
+import sys
+sys.path.append("../modules/")
+import ping
+from fake_tbot import TestedBot
+
+import unittest
+
+
+class TestPingModule(unittest.TestCase):
+
+    def setUp(self):
+        self.tbot = TestedBot()
+        self.tbot.register(ping.thanks_ants)
+
+    def test_thanks_ants(self):
+        self.tbot.bot_messages = []
+        self.tbot.listen("Afal", "#42", "!thanks ants")
+        self.assertEqual(self.tbot.last_message()[1], "Thanks ants... Thants")
+
+    def test_thanks_trtl(self):
+        self.tbot.bot_messages = []
+        self.tbot.listen("Afal", "#42", "!thanks trtl")
+        self.assertEqual(self.tbot.last_message()[1], "Thanks trtl... Thtrtl")
+
+    def test_thanks_sylnai(self):
+        self.tbot.bot_messages = []
+        self.tbot.listen("Afal", "#42", "!thanks Sylnai")
+        self.assertEqual(self.tbot.last_message()[1], "Thanks Sylnai... Thai")
+
+    def test_thanks_phoebe(self):
+        self.tbot.bot_messages = []
+        self.tbot.listen("Afal", "#42", "!thanks phoebe")
+        self.assertEqual(self.tbot.last_message()[1], "Thanks phoebe... Thoebe")
+
+if __name__ == '__main__':
+    unittest.main()
 """
 import os
 import sys
+import traceback
 import readline
 import re
 regex = re.compile("\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE)
         for function in dir(MODULES[module]):
             glob = MODULES[module].__dict__[function]
             if hasattr(glob, 'rule'):
-                TBOT.register(glob)
+                TBOT.register(glob, function)
     except:
         pass
 
         print "Bye!"
         sys.exit()
     if msg and msg[0] == "/":
-        (command, value) = msg.split(" ", 1)
+        try:
+            (command, value) = msg.split(" ", 1)
+        except:
+            command = msg
         if command == "/nick":
             USER = value
             print "(Nick has changed to '%s')" % USER
         elif command == "/j":
             CHANNEL = value
             print "(Channel has changed to '%s')" % CHANNEL
+        elif command == "/rules":
+            print "\n".join(TBOT.rules())
         else:
             print "(Not a recognised command)"
     else:
-        TBOT.listen(USER, CHANNEL, msg)
-        if [] != TBOT.bot_messages:
-            print regex.sub('', "\n".join([x[1] for x in TBOT.bot_messages]))
-            TBOT.bot_messages = []
+        try:
+            TBOT.listen(USER, CHANNEL, msg)
+            if [] != TBOT.bot_messages:
+                print regex.sub('', "\n".join([x[1] for x in TBOT.bot_messages]))
+                TBOT.bot_messages = []
+        except:
+            print traceback.format_exc()