From 956d5eef3fa88f05b7c7cf02f08c3e544c99bcc1 Mon Sep 17 00:00:00 2001 From: Robert Altner Date: Fri, 10 Jan 2020 19:39:45 +0100 Subject: [PATCH] added smartness --- api/thesaurus.py | 37 +++++++++++++++++++++++++++++++++++++ cogs/api/thesaurus.py | 18 ++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 api/thesaurus.py create mode 100644 cogs/api/thesaurus.py diff --git a/api/thesaurus.py b/api/thesaurus.py new file mode 100644 index 0000000..e240b05 --- /dev/null +++ b/api/thesaurus.py @@ -0,0 +1,37 @@ +import requests +import re, random +from util import logging, config + +key = config.settings["api_keys"]["thesaurus"] +url = f"http://thesaurus.altervista.org/thesaurus/v1?key={key}&language=en_US&output=json&word=" + +def thesaurufy_sentence(sentence): + symbols = re.findall(r"[\w']+|[.,!?;]", sentence) + + if len(symbols) == 0: + return "" + + for i in range(0, len(symbols)): + if not symbols[i].isalpha(): + continue + + response = requests.get(url + symbols[i]) + if not response.ok: + # logging.warning(f"Thesaurus API returned {response.status_code} ({url + symbols[i]})") + symbols[i] = " " + symbols[i] + continue + + responses = response.json()["response"] + words = responses[random.randint(0, len(responses) - 1)]["list"]["synonyms"].split("|") + word = words[random.randint(0, len(words) - 1)] + + if "(" in word: + if "antonym" in word.split("(")[1].lower(): + symbols[i] = " " + symbols[i] + continue + + word = word.split("(")[0][:-1] + + symbols[i] = " " + word + + return "".join(symbols) diff --git a/cogs/api/thesaurus.py b/cogs/api/thesaurus.py new file mode 100644 index 0000000..ffc81f5 --- /dev/null +++ b/cogs/api/thesaurus.py @@ -0,0 +1,18 @@ +import discord +from discord.ext import commands +from api import thesaurus + +class Thesaurus(commands.Cog): + + def __init__(self, client): + self.client = client + + @commands.command(name="thesaurus", description="Makes you smarter.", usage="thesaurus ", aliases=["saurus", "thes"]) + @commands.cooldown(1, 3) + async def thesaurus(self, ctx, *message): + loading = await ctx.send("Thinking...") + saurus = thesaurus.thesaurufy_sentence(" ".join(message)) + await loading.edit(content=saurus) + +def setup(client): + client.add_cog(Thesaurus(client)) \ No newline at end of file