Skip to content

Commit

Permalink
Update vocadb.py
Browse files Browse the repository at this point in the history
- Fix typing
- Replace footer language map with translationtype
- Now compatible with V3

Signed-off-by: Glas <[email protected]>
  • Loading branch information
DJTOMATO authored Apr 17, 2024
1 parent 5f2ccb4 commit f3223db
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions vocadb/vocadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ async def _fetch_data(self, ctx: commands.Context, query: str):
"User-Agent": f"Red-DiscordBot/{red_version} Fixator10-cogs/VocaDB/{self.__version__}"
}
try:
async with self.session.get(BASE_API_URL, params=params, headers=headers) as resp:
async with self.session.get(
BASE_API_URL, params=params, headers=headers
) as resp:
if resp.status != 200:
return f"https://http.cat/{resp.status}"
result = await resp.json()
Expand Down Expand Up @@ -123,40 +125,50 @@ def _info_embed(self, colour, data: Dict[str, Any]) -> discord.Embed:
embed.add_field(name="Duration", value=f"{minutes} minutes, {seconds} seconds")
favorites, score = (data.get("favoritedTimes", 0), data.get("ratingScore", 0))
embed.add_field(name="Published On", value=pub_date)
embed.add_field(name="Statistics", value=f"{favorites} favourite(s), {score} total score")
embed.add_field(
name="Statistics", value=f"{favorites} favourite(s), {score} total score"
)
embed.add_field(name="Artist(s)", value=all_artists)
embed.set_footer(text="Powered by VocaDB")
return embed

@staticmethod
def _lyrics_embed(colour, page: Dict[str, Any], data: Dict[str, Any]) -> discord.Embed:
def _lyrics_embed(
colour, page: Dict[str, Any], data: Dict[str, Any]
) -> discord.Embed:
"""Create an embed with the lyrics"""
print("Page:", page) # Add this debug print
title = [
x.get("value")
for x in data.get("names")
if x.get("language") == LANGUAGE_MAP.get(page["cultureCode"])
if x.get("language") == LANGUAGE_MAP.get(page.get("translationType"))
]

print("Title:", title) # Add this debug print
em = discord.Embed(
title=title[0] if title else data.get("defaultName"),
colour=colour,
)
em.set_thumbnail(url=data.get("thumbUrl") or "")
if data.get("id"):
em.url = f"https://vocadb.net/S/{data['id']}"
em.description = page["value"][:4090] if page.get("value") else "No lyrics found."
em.description = (
page["value"][:4090] if page.get("value") else "No lyrics found."
)
if page.get("url"):
em.add_field(
name="Source",
value=f"[{page.get('source') or 'Source'}]({page['url']})",
)

return em

@commands.command()
@commands.bot_has_permissions(embed_links=True)
@commands.cooldown(1, 5, commands.BucketType.user)
async def vocadb(self, ctx: commands.Context, *, query: str):
"""Fetch Vocaloid song lyrics from VocaDB.net database"""
await ctx.trigger_typing()
await ctx.channel.typing()
data = await self._fetch_data(ctx, query)

if type(data) == str:
Expand All @@ -170,10 +182,12 @@ async def vocadb(self, ctx: commands.Context, *, query: str):

embeds = []
for i, page in enumerate(data["lyrics"], start=1):
language = f"Language: {LANGUAGE_MAP.get(page.get('cultureCode', 'na'))}"
language = f"Language: {page.get('translationType', 'na')}"
emb = self._lyrics_embed(await ctx.embed_colour(), page, data)
emb.set_footer(text=f"{language} • Page {i} of {len(data['lyrics'])}")
embeds.append(emb)

controls = {"\N{CROSS MARK}": close_menu} if len(embeds) == 1 else DEFAULT_CONTROLS
controls = (
{"\N{CROSS MARK}": close_menu} if len(embeds) == 1 else DEFAULT_CONTROLS
)
await menu(ctx, embeds, controls=controls, timeout=90.0)

0 comments on commit f3223db

Please sign in to comment.