mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[General] UI improvements to [p]urban (#1871)
* Changed urban dictionary for my embed version * Used black for formatting * Fixed everything according to Tobotimus's review * Fixed the description limit to 2048 characters * Better fix adding "..." at the end if neccessary * Add non-embed version * Blackify
This commit is contained in:
parent
cf7cafc261
commit
9d4f9ef73c
@ -3,12 +3,11 @@ import time
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from random import randint, choice
|
from random import randint, choice
|
||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
from redbot.core import commands
|
from redbot.core import commands
|
||||||
from redbot.core.i18n import Translator, cog_i18n
|
from redbot.core.i18n import Translator, cog_i18n
|
||||||
|
from redbot.core.utils.menus import menu, DEFAULT_CONTROLS
|
||||||
from redbot.core.utils.chat_formatting import escape, italics, pagify
|
from redbot.core.utils.chat_formatting import escape, italics, pagify
|
||||||
|
|
||||||
_ = Translator("General", __file__)
|
_ = Translator("General", __file__)
|
||||||
@ -224,49 +223,89 @@ class General:
|
|||||||
await ctx.send(_("I need the `Embed links` permission to send this."))
|
await ctx.send(_("I need the `Embed links` permission to send this."))
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def urban(self, ctx, *, search_terms: str, definition_number: int = 1):
|
async def urban(self, ctx, *, word):
|
||||||
"""Urban Dictionary search
|
"""Searches urban dictionary entries using the unofficial api"""
|
||||||
|
|
||||||
Definition number must be between 1 and 10"""
|
|
||||||
|
|
||||||
def encode(s):
|
|
||||||
return quote_plus(s, encoding="utf-8", errors="replace")
|
|
||||||
|
|
||||||
# definition_number is just there to show up in the help
|
|
||||||
# all this mess is to avoid forcing double quotes on the user
|
|
||||||
|
|
||||||
search_terms = search_terms.split(" ")
|
|
||||||
try:
|
try:
|
||||||
if len(search_terms) > 1:
|
url = "https://api.urbandictionary.com/v0/define?term=" + str(word).lower()
|
||||||
pos = int(search_terms[-1]) - 1
|
|
||||||
search_terms = search_terms[:-1]
|
headers = {"content-type": "application/json"}
|
||||||
else:
|
|
||||||
pos = 0
|
|
||||||
if pos not in range(0, 11): # API only provides the
|
|
||||||
pos = 0 # top 10 definitions
|
|
||||||
except ValueError:
|
|
||||||
pos = 0
|
|
||||||
|
|
||||||
search_terms = {"term": "+".join([s for s in search_terms])}
|
|
||||||
url = "http://api.urbandictionary.com/v0/define"
|
|
||||||
try:
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(url, params=search_terms) as r:
|
async with session.get(url, headers=headers) as response:
|
||||||
result = await r.json()
|
data = await response.json()
|
||||||
item_list = result["list"]
|
|
||||||
if item_list:
|
|
||||||
definition = item_list[pos]["definition"]
|
|
||||||
example = item_list[pos]["example"]
|
|
||||||
defs = len(item_list)
|
|
||||||
msg = "**Definition #{} out of {}:\n**{}\n\n**Example:\n**{}".format(
|
|
||||||
pos + 1, defs, definition, example
|
|
||||||
)
|
|
||||||
msg = pagify(msg, ["\n"])
|
|
||||||
for page in msg:
|
|
||||||
await ctx.send(page)
|
|
||||||
else:
|
|
||||||
await ctx.send(_("Your search terms gave no results."))
|
|
||||||
except IndexError:
|
|
||||||
await ctx.send(_("There is no definition #{}").format(pos + 1))
|
|
||||||
except:
|
except:
|
||||||
await ctx.send(_("Error."))
|
await ctx.send(
|
||||||
|
_(("No Urban dictionary entries were found or there was an error in the process"))
|
||||||
|
)
|
||||||
|
|
||||||
|
if data.get("error") != 404:
|
||||||
|
|
||||||
|
if await ctx.embed_requested():
|
||||||
|
# a list of embeds
|
||||||
|
embeds = []
|
||||||
|
for ud in data["list"]:
|
||||||
|
embed = discord.Embed()
|
||||||
|
embed.title = _("{} by {}".format(ud["word"].capitalize(), ud["author"]))
|
||||||
|
embed.url = ud["permalink"]
|
||||||
|
|
||||||
|
description = "{} \n \n **Example : ** {}".format(
|
||||||
|
ud["definition"], ud.get("example", "N/A")
|
||||||
|
)
|
||||||
|
if len(description) > 2048:
|
||||||
|
description = "{}...".format(description[:2045])
|
||||||
|
embed.description = _(description)
|
||||||
|
|
||||||
|
embed.set_footer(
|
||||||
|
text=_(
|
||||||
|
"{} Down / {} Up , Powered by urban dictionary".format(
|
||||||
|
ud["thumbs_down"], ud["thumbs_up"]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
embeds.append(embed)
|
||||||
|
|
||||||
|
if embeds is not None and len(embeds) > 0:
|
||||||
|
await menu(
|
||||||
|
ctx,
|
||||||
|
pages=embeds,
|
||||||
|
controls=DEFAULT_CONTROLS,
|
||||||
|
message=None,
|
||||||
|
page=0,
|
||||||
|
timeout=30,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
messages = []
|
||||||
|
for ud in data["list"]:
|
||||||
|
description = "{} \n \n **Example : ** {}".format(
|
||||||
|
ud["definition"], ud.get("example", "N/A")
|
||||||
|
)
|
||||||
|
if len(description) > 2048:
|
||||||
|
description = "{}...".format(description[:2045])
|
||||||
|
description = _(description)
|
||||||
|
|
||||||
|
message = "<{}> \n {} by {} \n \n {} \n \n {} Down / {} Up , Powered by urban dictionary".format(
|
||||||
|
ud["permalink"],
|
||||||
|
ud["word"].capitalize(),
|
||||||
|
ud["author"],
|
||||||
|
description,
|
||||||
|
ud["thumbs_down"],
|
||||||
|
ud["thumbs_up"],
|
||||||
|
)
|
||||||
|
messages.append(message)
|
||||||
|
|
||||||
|
if messages is not None and len(messages) > 0:
|
||||||
|
await menu(
|
||||||
|
ctx,
|
||||||
|
pages=messages,
|
||||||
|
controls=DEFAULT_CONTROLS,
|
||||||
|
message=None,
|
||||||
|
page=0,
|
||||||
|
timeout=30,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await ctx.send(
|
||||||
|
_(("No Urban dictionary entries were found or there was an error in the process"))
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user