move define to be a slash command

This commit is contained in:
Luke Robles 2022-11-18 09:05:29 -08:00
parent e08a376154
commit e07335269e
3 changed files with 50 additions and 43 deletions

View File

@ -56,6 +56,23 @@ class ActualUtils(commands.Cog):
await ctx.respond(r.json()["web_url"]) 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") @commands.command(name="tts")
async def tts(self, ctx: commands.Context): async def tts(self, ctx: commands.Context):

View File

@ -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") @commands.command(name="wallpaper")
async def wallpaper(self, ctx: commands.Context): async def wallpaper(self, ctx: commands.Context):
import wallpaper import wallpaper

View File

@ -4,52 +4,49 @@ import help_methods
import discord 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: try:
word = content.split()[1:] 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:
# Try another dictionary
definition = requests.get( definition = requests.get(
"https://api.urbandictionary.com/v0/define?term={}".format( "http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword={}".format(
"%20".join(word) "%20".join(word)
) )
).json()["list"] ).json()["results"][0]["senses"][0]["definition"][0]
# 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"
site = "Pearson"
except IndexError: except IndexError:
try: definition = "No definition found"
# 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]
site = "Pearson" embed = discord.Embed(
except IndexError: description="%s:\n%s" % (" ".join(word), definition),
definition = "No definition found" color=0x428BCA,
type="rich",
)
embed.set_author(name="From %s" % site)
embed = discord.Embed( return embed
description="%s:\n%s" % (" ".join(word), definition),
color=0x428BCA,
type="rich",
)
embed.set_author(name="From %s" % site)
return embed
return help_methods.get_help_message("define") return help_methods.get_help_message("define")