X-Git-Url: https://git.armaanb.net/?p=tall.bot.git;a=blobdiff_plain;f=tallbot.py;h=fba1c1639eb445f2ec52de26a9f36e087525e174;hp=371c048bc9162cc6aa1423d8fd1382680d0a4d62;hb=da004a969e475c8a39eb9c652b0706b5b4a92a08;hpb=36f2be356169528b44d0904fd652fc0be5b1fa51 diff --git a/tallbot.py b/tallbot.py index 371c048..fba1c16 100755 --- a/tallbot.py +++ b/tallbot.py @@ -1,46 +1,96 @@ #!/usr/bin/env python3 # tall.bot - a simple Discord Wikipedia bot -# Armaan Bhojwani 2020 +# Armaan Bhojwani 2021 import logging +import sqlite3 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, pid): - search = " ".join(search) try: - logging.info(str(pid) + " | " + search) + logging.info(str(pid) + " | " + str(int(time.time())) + " | " + str(search)) + log_query(search, pid) return " " + wikipedia.summary(search, sentences=sen, auto_suggest=False) except Exception as e: - logging.debug(str(pid) + " | " + search + " | " + str(e)) + logging.debug(str(pid) + " | " + str(search) + " | " + str(e)) return str(e) -def keyword_check(inp): +def create_query(inp): + key1 = ["What", "what", "who", "Who", "what's", "What's", "whats", "Whats", + "Whatsa", "whatsa"] + key2 = ["is", "are", "were", "was"] + key3 = ["a", "an", "the"] + 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 + + return " ".join(x) + +def tell_more(inp, pid): + def find_query(inp): + key1 = ["Tell me more", "tell me more"] + if inp[:12] in key1: + return inp[11:] + else: + return False + + def query_lookup(pid): + old = int(time.time()) - 150 + c.execute("SELECT query FROM queries WHERE pid = (?) AND time > (?)", + (str(pid), old)) + + (query, *_) = c.fetchone() + return str(query) + + if find_query(inp): + return query_lookup(pid) else: return False +def prep_database(): + c.execute("""CREATE TABLE IF NOT EXISTS queries( + time INTEGER, + pid TEXT, + query TEXT)""") + conn.commit() + +def log_query(search, pid): + c.execute("DELETE FROM queries WHERE pid = (?)", (str(pid),)) + c.execute("""INSERT INTO queries + (time, pid, query) VALUES(?, ?, ?)""", + (int(time.time()), str(pid), str(search))) + conn.commit() + class TallBot(discord.Client): async def on_ready(self): - logging.debug('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, - message.author)) + query = create_query(message.content) + more_info = tell_more(message.content, message.author) + if query: + await message.channel.send(wiki_sum(query, 1, message.author)) + elif more_info: + await message.channel.send(wiki_sum(more_info, 6, message.author)) + +def main(): + prep_database() + TallBot().run(sys.argv[1]) -TallBot().run(sys.argv[1]) +if __name__ == "__main__": + conn = sqlite3.connect("queries.db") + c = conn.cursor() + main()