Kanji now uses interactive

This commit is contained in:
Robert 2021-02-08 17:38:52 +01:00
parent 65fe559ece
commit 387fd75b38

View file

@ -1,41 +1,47 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from utils import jisho from utils import jisho, interactive
class JishoKanjiObject(): class KanjiEmbed(interactive.InteractiveEmbed):
def __init__(self, query, owner): REACTIONS = {
self.response = jisho.JishoKanji(query) "prev_kanji": "⬅️",
self.total_pages = self.response.entries "next_kanji": "➡️"
self.page = 0 }
self.owner = owner
def prev(self): def __init__(self, parent, ctx, response):
self.page -= 1 super(KanjiEmbed, self).__init__(parent.bot, ctx, 60.0)
if self.page < 0: self.parent = parent
self.page = self.total_pages - 1 self.owner = self.ctx.author
self.response = response
def next(self): self.current_kanji = 0
self.page += 1
if self.page >= self.total_pages:
self.page = 0
def prev_kanji(self):
self.current_kanji -= 1
if self.current_kanji < 0:
self.current_kanji = self.response.entries - 1
class Kanji(commands.Cog): def next_kanji(self):
def __init__(self, bot): self.current_kanji += 1
self.bot = bot if self.current_kanji >= self.response.entries:
self.activeObject = None self.current_kanji = 0
self.latestMessage = 0
async def createEmbed(self): async def on_reaction(self, reaction, user):
if self.activeObject.total_pages == 0: if reaction.emoji == KanjiEmbed.REACTIONS["prev_kanji"]:
embed = discord.Embed( self.prev_kanji()
title = "No search results", await reaction.remove(user)
description = "The search returned nothing. Did you make a typo?",
colour = 0x56d926 if reaction.emoji == KanjiEmbed.REACTIONS["next_kanji"]:
) self.next_kanji()
else: await reaction.remove(user)
response = self.activeObject.response
node = response.nodes[self.activeObject.page] 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"])
def make_embed(self):
node = self.response.nodes[self.current_kanji]
embed = discord.Embed( embed = discord.Embed(
title = node.meaning, title = node.meaning,
@ -50,41 +56,30 @@ class Kanji(commands.Cog):
embed.add_field(name="On", value="".join(node.on), inline=False) embed.add_field(name="On", value="".join(node.on), inline=False)
embed.add_field(name=f"Radical: {node.radical[0]}", value=node.radical[1], inline=False) embed.add_field(name=f"Radical: {node.radical[0]}", value=node.radical[1], inline=False)
embed.set_thumbnail(url=node.image_url) 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}")
embed.set_footer(text=f"Jōyō kanji (Grade {node.grade}) | JLPT level {node.jlpt}\t\t{self.activeObject.page + 1}/{self.activeObject.total_pages}")
return embed return embed
@commands.Cog.listener() async def on_close(self):
async def on_reaction_add(self, reaction, user): self.parent.activeObjects.pop(self.ctx.channel.id)
message = reaction.message
if message.id != self.latestMessage:
return
if user == self.bot.user:
return
if user.id != self.activeObject.owner: class Kanji(commands.Cog):
return def __init__(self, bot):
self.bot = bot
if reaction.me: self.activeObjects = {}
if reaction.emoji == "⬅️":
self.activeObject.prev()
await reaction.remove(user)
if reaction.emoji == "➡️":
self.activeObject.next()
await reaction.remove(user)
embed = await self.createEmbed()
await message.edit(embed=embed)
@commands.command(name="kanji", description="Performs a Kanji search", usage="<kanji>", aliases=["k"]) @commands.command(name="kanji", description="Performs a Kanji search", usage="<kanji>", aliases=["k"])
@commands.cooldown(1, 5) @commands.cooldown(1, 5)
async def kanji(self, ctx, *, kanji: str = None): async def kanji(self, ctx, *, kanji: str = None):
if kanji is None: if kanji is None:
embed = discord.Embed(
title = "No search results",
description = "The search returned nothing. Did you make a typo?",
colour = 0x56d926
)
await ctx.send(embed=embed)
return return
embed = discord.Embed( embed = discord.Embed(
@ -92,19 +87,23 @@ class Kanji(commands.Cog):
description = "This might take a few seconds", description = "This might take a few seconds",
colour = 0x56d926 colour = 0x56d926
) )
message = await ctx.send(embed=embed) message = await ctx.send(embed=embed)
kanji = kanji[:5] kanji = kanji[:5]
response = jisho.JishoKanji(kanji)
self.activeObject = JishoKanjiObject(kanji, ctx.author.id) if response.entries < 1:
embed = await self.createEmbed() embed = discord.Embed(
title = "No search results",
description = "The search returned nothing. Did you make a typo?",
colour = 0x56d926
)
await message.edit(embed=embed) await message.edit(embed=embed)
self.latestMessage = message.id return
if self.activeObject.total_pages > 1: await message.delete()
await message.add_reaction("⬅️") self.activeObjects[ctx.channel.id] = KanjiEmbed(self, ctx, response)
await message.add_reaction("➡️") await self.activeObjects[ctx.channel.id].show_embed()
def setup(bot): def setup(bot):
bot.add_cog(Kanji(bot)) bot.add_cog(Kanji(bot))