JishoBot/cogs/kanji.py

109 lines
3.5 KiB
Python
Raw Normal View History

2020-08-11 13:52:13 +00:00
import discord
from discord.ext import commands
2021-02-08 16:38:52 +00:00
from utils import jisho, interactive
2020-08-11 13:52:13 +00:00
2021-02-08 16:38:52 +00:00
class KanjiEmbed(interactive.InteractiveEmbed):
REACTIONS = {
"prev_kanji": "⬅️",
"next_kanji": "➡️"
}
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
def __init__(self, parent, ctx, response):
super(KanjiEmbed, self).__init__(parent.bot, ctx, 60.0)
self.parent = parent
self.owner = self.ctx.author
self.response = response
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
self.current_kanji = 0
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
def prev_kanji(self):
self.current_kanji -= 1
if self.current_kanji < 0:
self.current_kanji = self.response.entries - 1
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
def next_kanji(self):
self.current_kanji += 1
if self.current_kanji >= self.response.entries:
self.current_kanji = 0
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
async def on_reaction(self, reaction, user):
if reaction.emoji == KanjiEmbed.REACTIONS["prev_kanji"]:
self.prev_kanji()
await reaction.remove(user)
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
if reaction.emoji == KanjiEmbed.REACTIONS["next_kanji"]:
self.next_kanji()
await reaction.remove(user)
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
async def add_navigation(self, message):
if self.response.entries > 1:
await message.add_reaction(KanjiEmbed.REACTIONS["prev_kanji"])
await message.add_reaction(KanjiEmbed.REACTIONS["next_kanji"])
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
def make_embed(self):
node = self.response.nodes[self.current_kanji]
2020-08-11 16:31:20 +00:00
2021-02-08 16:38:52 +00:00
embed = discord.Embed(
title = node.meaning,
url = node.url,
description = f"{node.strokes} strokes",
colour = 0x56d926
)
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
if node.kun:
embed.add_field(name="Kun", value="".join(node.kun), inline=False)
if node.on:
embed.add_field(name="On", value="".join(node.on), inline=False)
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
embed.add_field(name=f"Radical: {node.radical[0]}", value=node.radical[1], inline=False)
embed.set_thumbnail(url=node.image_url)
embed.set_footer(text=f"Jōyō kanji (Grade {node.grade}) | JLPT level {node.jlpt}\t\t{self.current_kanji + 1}/{self.response.entries}")
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
return embed
2020-08-11 16:07:31 +00:00
2021-02-08 16:38:52 +00:00
async def on_close(self):
self.parent.activeObjects.pop(self.ctx.channel.id)
2020-08-11 15:34:22 +00:00
2021-02-08 16:38:52 +00:00
class Kanji(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.activeObjects = {}
2020-08-11 13:52:13 +00:00
@commands.command(name="kanji", description="Performs a Kanji search", usage="<kanji>", aliases=["k"])
@commands.cooldown(1, 5)
2020-08-11 14:56:19 +00:00
async def kanji(self, ctx, *, kanji: str = None):
if kanji is None:
2021-02-08 16:38:52 +00:00
embed = discord.Embed(
title = "No search results",
description = "The search returned nothing. Did you make a typo?",
colour = 0x56d926
)
await ctx.send(embed=embed)
2020-08-11 14:56:19 +00:00
return
2020-08-11 17:11:47 +00:00
embed = discord.Embed(
title = "Loading...",
description = "This might take a few seconds",
colour = 0x56d926
)
message = await ctx.send(embed=embed)
2020-08-11 16:50:56 +00:00
kanji = kanji[:5]
2021-02-08 16:38:52 +00:00
response = jisho.JishoKanji(kanji)
if response.entries < 1:
embed = discord.Embed(
title = "No search results",
description = "The search returned nothing. Did you make a typo?",
colour = 0x56d926
)
await message.edit(embed=embed)
return
2020-08-11 16:50:56 +00:00
2021-02-08 16:38:52 +00:00
await message.delete()
self.activeObjects[ctx.channel.id] = KanjiEmbed(self, ctx, response)
await self.activeObjects[ctx.channel.id].show_embed()
2020-08-11 13:52:13 +00:00
def setup(bot):
bot.add_cog(Kanji(bot))