]> git.armaanb.net Git - tall.bot.git/blobdiff - tallbot.py
add detection for words like "the" in the query
[tall.bot.git] / tallbot.py
index a888a4ee16760fa6156d32bdead675bde2fabaed..e097ba78234fbc75146932e3f8af73d2ad11cc98 100755 (executable)
@@ -2,40 +2,51 @@
 # tall.bot - a simple Discord Wikipedia bot
 # Armaan Bhojwani 2020
 
+import logging
 import sys
+import time
 import wikipedia
 import discord
 
-keywords = ["what is", "What is", "what was", "What was", "what were",
-            "What were", "who is", "Who is", "who was", "Who was", 
-            "who were", "Who were", "who are", "Who are"]
-
-def wiki_sum(search, sen):
+def wiki_sum(search, sen, pid):
     try:
-        output = "From Wikipedia: " + wikipedia.summary(search, sentences=sen)
-    except:
-        output = "There was an error"
-
-    return output
+        logging.info(str(pid) + " | " + str(int(time.time())) + " | " + search)
+        return "<Wikipedia> " + wikipedia.summary(search,
+                                                  sentences=sen,
+                                                  auto_suggest=False)
+    except Exception as e:
+        logging.debug(str(pid) + " | " + search + " | " + str(e))
+        return str(e)
+
+def create_query(inp):
+    key1 = ["What", "what", "who", "Who"]
+    key2 = ["is", "are", "were", "was"]
+    key3 = ["a", "an", "the"]
 
-def keyword_check(inp):
     x = inp.split()
-    x = " ".join(x[:2])
-    if x in keywords:
-        return True
+    if x[0] in key1 and x[1] in key2:
+        x = x[2:]
+        if x[0] in key3:
+            x = x[1:]
     else:
         return False
 
-class MyClient(discord.Client):
+    return " ".join(x)
+
+class TallBot(discord.Client):
     async def on_ready(self):
-        print('Logged in as', self.user)
+        logging.info('Logged in as ', self.user)
 
     async def on_message(self, message):
         if message.author == self.user:
             return
 
-        if keyword_check(message.content):
-            await message.channel.send(wiki_sum(message.content.split()[2:], 2))
+        key = create_query(message.content)
+        if key:
+            await message.channel.send(wiki_sum(key, 2, message.author))
+
+def main():
+    TallBot().run(sys.argv[1])
 
-client = MyClient()
-client.run(sys.argv[1])
+if __name__ == "__main__":
+    main()