[Dictionary] Pagify results

This commit is contained in:
aikaterna
2020-09-09 10:17:01 -07:00
committed by GitHub
parent 559f7049db
commit c94ead0429

View File

@@ -3,6 +3,7 @@ from bs4 import BeautifulSoup
import logging
import re
from redbot.core import commands
from redbot.core.utils.chat_formatting import pagify
log = logging.getLogger("red.aikaterna.dictionary")
@@ -34,14 +35,16 @@ class Dictionary(commands.Cog):
@commands.command()
async def antonym(self, ctx, *, word: str):
"""Displays antonyms for a given word."""
search_msg = await ctx.send("Searching...")
search_term = word.split(" ", 1)[0]
result = await self._antonym(ctx, search_term)
if not result:
return await search_msg.edit(content="This word is not in the dictionary.")
await ctx.send("This word is not in the dictionary.")
return
result_text = "*, *".join(result)
await search_msg.edit(content=f"Antonyms for **{search_term}**: *{result_text}*")
msg = f"Antonyms for **{search_term}**: *{result_text}*"
for page in pagify(msg, delims=["\n"]):
await ctx.send(page)
async def _antonym(self, ctx, word):
data = await self._get_soup_object(f"http://www.thesaurus.com/browse/{word}")
@@ -64,7 +67,9 @@ class Dictionary(commands.Cog):
result = await self._definition(ctx, search_term)
str_buffer = ""
if not result:
return await search_msg.edit(content="This word is not in the dictionary.")
await search_msg.delete()
await ctx.send("This word is not in the dictionary.")
return
for key in result:
str_buffer += f"\n**{key}**: \n"
counter = 1
@@ -81,7 +86,9 @@ class Dictionary(commands.Cog):
else:
str_buffer += f"{str(counter)}. {val}\n"
counter += 1
await search_msg.edit(content=str_buffer)
await search_msg.delete()
for page in pagify(str_buffer, delims=["\n"]):
await ctx.send(page)
async def _definition(self, ctx, word):
data = await self._get_soup_object(f"http://wordnetweb.princeton.edu/perl/webwn?s={word}")
@@ -121,11 +128,13 @@ class Dictionary(commands.Cog):
@commands.command()
async def synonym(self, ctx, *, word: str):
"""Displays synonyms for a given word."""
search_msg = await ctx.send("Searching...")
search_term = word.split(" ", 1)[0]
result = await self._synonym(ctx, search_term)
if not result:
return await search_msg.edit(content="This word is not in the dictionary.")
await ctx.send("This word is not in the dictionary.")
return
result_text = "*, *".join(result)
await search_msg.edit(content=f"Synonyms for **{search_term}**: *{result_text}*")
msg = f"Synonyms for **{search_term}**: *{result_text}*"
for page in pagify(msg, delims=["\n"]):
await ctx.send(page)