move define to be a slash command
This commit is contained in:
parent
e08a376154
commit
e07335269e
@ -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):
|
||||
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user