Embed done

This commit is contained in:
Robert 2020-08-11 13:11:46 +02:00
parent 88d7ea0ac4
commit 86986f977b
2 changed files with 64 additions and 4 deletions

View file

@ -2,9 +2,47 @@ import discord
from discord.ext import commands
from utils import jisho
class JishoObject():
def __init__(self, query):
self.response = jisho.JishoResponse(query)
self.total_pages = self.response.entries
self.page = 0
class Search(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.activeObject = None
async def createEmbed(self):
response = self.activeObject.response
node = response.nodes[self.activeObject.page]
embed = discord.Embed(
title = node.japanese[0][0],
url = f"https://jisho.org/word/{node.slug}",
description = node.japanese[0][1],
colour = 0x56d926
)
i = 1
for sense in node.senses:
embed.add_field(name=f"{i}. {sense.fenglish_definitions}", value=sense.fparts_of_speech, inline=False)
i += 1
if len(node.japanese) > 1:
other = ""
for word, reading in node.japanese[1:]:
other += word
if reading != "":
other += f"{reading}"
other += "\n"
embed.add_field(name="Other forms", value=other)
embed.set_footer(
text = node.ftags
)
return embed
@commands.command(name="search", description="Searches Jisho", usage="<query>", aliases=["s"])
@commands.cooldown(1, 5)
@ -12,8 +50,11 @@ class Search(commands.Cog):
if query == None:
return
response = jisho.JishoResponse(query)
await ctx.send(response.nodes[0].slug)
self.activeObject = JishoObject(query)
embed = await self.createEmbed()
message = await ctx.send(embed=embed)
await message.add_reaction("⬅️")
await message.add_reaction("➡️")
@search.error
async def search_error(self, ctx, error):

View file

@ -7,20 +7,39 @@ TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}"
class JishoSenses():
def __init__(self, sense):
self.english_definitions = sense["english_definitions"]
self.english_definitions_formatted = "; ".join(self.english_definitions)
self.fenglish_definitions = "; ".join(self.english_definitions)
self.parts_of_speech = sense["parts_of_speech"]
self.fparts_of_speech = ", ".join(sense["parts_of_speech"])
if self.fparts_of_speech == "":
self.fparts_of_speech = "\u200b" # Zero width space to have empty embed value
class JishoNode():
def __init__(self, node):
self.slug = node["slug"]
self.is_common = node["is_common"]
if "is_common" in node:
self.is_common = node["is_common"]
else:
self.is_common = False
self.tags = node["tags"]
self.jlpt = node["jlpt"]
self.ftags = ""
if self.is_common:
self.ftags += "Common "
for tag in self.tags:
self.ftags += f"| {tag} "
for jlpt in self.jlpt:
self.ftags += f"| {jlpt} "
self.japanese = []
for entry in node["japanese"]:
if "word" not in entry:
word = entry["reading"]
reading = ""
elif "reading" not in entry:
word = entry["word"]
reading = ""
else:
word = entry["word"]
reading = entry["reading"]