diff --git a/app/cogs/actual_utils.py b/app/cogs/actual_utils.py index f6612b8e..054615c4 100644 --- a/app/cogs/actual_utils.py +++ b/app/cogs/actual_utils.py @@ -56,6 +56,23 @@ class ActualUtils(commands.Cog): await ctx.respond(r.json()["web_url"]) + @commands.slash_command( + guild_ids=None, + name="define", + description="Grabs the highest rated definition from urban dictionary", + ) + @option( + name="word", + description="The word to define. Tries UD first then an actual dictionary", + required=True, + ) + async def define(self, ctx, word): + + import define_word + + embed = define_word.get_definition(word) + await ctx.respond(embed=embed) + @commands.command(name="tts") async def tts(self, ctx: commands.Context): diff --git a/app/cogs/cheeky_functions.py b/app/cogs/cheeky_functions.py index a6be9282..5569b76c 100644 --- a/app/cogs/cheeky_functions.py +++ b/app/cogs/cheeky_functions.py @@ -133,13 +133,6 @@ class Cheeky(commands.Cog): ) ) - @commands.command(name="define", aliases=["ud"]) - async def define(self, ctx: commands.Context): - - import define_word - - await ctx.send(embed=define_word.get_definition(ctx.message.content)) - @commands.command(name="wallpaper") async def wallpaper(self, ctx: commands.Context): import wallpaper diff --git a/app/define_word.py b/app/define_word.py index c0542418..5e1ae641 100755 --- a/app/define_word.py +++ b/app/define_word.py @@ -4,52 +4,49 @@ import help_methods import discord -def get_definition(content): +def get_definition(word): """ - get_definition(content) + get_definition(word) - grabs a definition from urban dictionary + Tries to return the highest voted definition from urbandictionary. + If that fails, it reaches out to pearson for an actual defintion """ - if len(content.split()) > 1: - word = content.split()[1:] + try: + definition = requests.get( + "https://api.urbandictionary.com/v0/define?term={}".format("%20".join(word)) + ).json()["list"] + # UD returns all the results in a json blob. Calculate which result has the highest upvotes to downvotes ratio + # as this is the result that shows up at the top on urbandictionary.com + ratios = {} + for result in definition: + upvotes = result["thumbs_up"] + downvotes = result["thumbs_down"] + if int(downvotes) > 0: + ratio = int(upvotes) / int(downvotes) + ratios[definition.index(result)] = ratio + definition = definition[(max(ratios, key=ratios.get))]["definition"] + site = "Urban Dictionary" + + except IndexError: try: + # Try another dictionary definition = requests.get( - "https://api.urbandictionary.com/v0/define?term={}".format( + "http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword={}".format( "%20".join(word) ) - ).json()["list"] - # UD returns all the results in a json blob. Calculate which result has the highest upvotes to downvotes ratio - # as this is the result that shows up at the top on urbandictionary.com - ratios = {} - for result in definition: - upvotes = result["thumbs_up"] - downvotes = result["thumbs_down"] - if int(downvotes) > 0: - ratio = int(upvotes) / int(downvotes) - ratios[definition.index(result)] = ratio - definition = definition[(max(ratios, key=ratios.get))]["definition"] - site = "Urban Dictionary" + ).json()["results"][0]["senses"][0]["definition"][0] + site = "Pearson" except IndexError: - try: - # Try another dictionary - definition = requests.get( - "http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword={}".format( - "%20".join(word) - ) - ).json()["results"][0]["senses"][0]["definition"][0] + definition = "No definition found" - site = "Pearson" - except IndexError: - definition = "No definition found" + embed = discord.Embed( + description="%s:\n%s" % (" ".join(word), definition), + color=0x428BCA, + type="rich", + ) + embed.set_author(name="From %s" % site) - embed = discord.Embed( - description="%s:\n%s" % (" ".join(word), definition), - color=0x428BCA, - type="rich", - ) - embed.set_author(name="From %s" % site) - - return embed + return embed return help_methods.get_help_message("define")