]> git.armaanb.net Git - tall.bot.git/blob - tallbot.py
add detection for words like "the" in the query
[tall.bot.git] / tallbot.py
1 #!/usr/bin/env python3
2 # tall.bot - a simple Discord Wikipedia bot
3 # Armaan Bhojwani 2020
4
5 import logging
6 import sys
7 import time
8 import wikipedia
9 import discord
10
11 def wiki_sum(search, sen, pid):
12     try:
13         logging.info(str(pid) + " | " + str(int(time.time())) + " | " + search)
14         return "<Wikipedia> " + wikipedia.summary(search,
15                                                   sentences=sen,
16                                                   auto_suggest=False)
17     except Exception as e:
18         logging.debug(str(pid) + " | " + search + " | " + str(e))
19         return str(e)
20
21 def create_query(inp):
22     key1 = ["What", "what", "who", "Who"]
23     key2 = ["is", "are", "were", "was"]
24     key3 = ["a", "an", "the"]
25
26     x = inp.split()
27     if x[0] in key1 and x[1] in key2:
28         x = x[2:]
29         if x[0] in key3:
30             x = x[1:]
31     else:
32         return False
33
34     return " ".join(x)
35
36 class TallBot(discord.Client):
37     async def on_ready(self):
38         logging.info('Logged in as ', self.user)
39
40     async def on_message(self, message):
41         if message.author == self.user:
42             return
43
44         key = create_query(message.content)
45         if key:
46             await message.channel.send(wiki_sum(key, 2, message.author))
47
48 def main():
49     TallBot().run(sys.argv[1])
50
51 if __name__ == "__main__":
52     main()