Adding lyrics

This commit is contained in:
Mylloon 2021-08-10 10:42:58 +02:00
parent 9bc5336257
commit cae2f4444c

View file

@ -27,7 +27,14 @@ import re
import typing import typing
import wavelink import wavelink
from discord.ext import commands, menus from discord.ext import commands, menus
from utils.core import load
# Genius API
from lyricsgenius import Genius
from utils.core import ligneFormatage, userOrNick, load
from utils.time import nowCustom
genius = Genius(load(["TOKEN_GENIUS"])["TOKEN_GENIUS"])
genius.response_format = "markdown"
def setup(client): def setup(client):
client.add_cog(Music(client)) client.add_cog(Music(client))
@ -750,3 +757,71 @@ class Music(commands.Cog, wavelink.WavelinkMixin):
else: else:
player.dj = m player.dj = m
return await ctx.send(f'{member.mention} is now the DJ.') return await ctx.send(f'{member.mention} is now the DJ.')
@commands.command(name='lyrics', aliases = ['l', 'lyric'])
async def _lyrics(self, ctx, *, song: str = None):
"""Affiche les paroles de la musique en cours, ou de la chanson spécifiée.\n ➡ Syntaxe: {PREFIX}lyrics/lyric/l (musique)"""
player: Player = self.bot.wavelink.get_player(guild_id=ctx.guild.id, cls=Player, context=ctx)
if song or player.is_playing:
if not song:
song = re.sub(r"(\ )?\(.*\)", "", player.current.title)
if " romanized" in song:
message = await ctx.send(f":mag: **Cherche les paroles romanisées de ** `{song.replace(' romanized', '')}`")
else:
message = await ctx.send(f":mag: **Cherche les paroles de ** `{song}`")
temps_requete = int(round(nowCustom() * 1000))
song_genius = genius.search_song(song)
couleur_embed = discord.Colour.random()
try:
paroles = song_genius.lyrics
except:
await ctx.message.add_reaction(emoji = '')
return await message.edit(content = f"Pas de résultats trouvés pour `{song}`.")
paroles = re.sub(r"3?EmbedShare URLCopyEmbedCopy", "", paroles) # Fix temporaire bug Genius
lignetotal = ""
premierembed = True
if len(paroles) > 7500:
await ctx.message.add_reaction(emoji = '')
return await message.edit(content = f"Désolé, les paroles sont trop longues pour être affichés (lien vers la page des paroles : {song_genius.url}).")
title_first_embed = f"Paroles de {song_genius.title} par {song_genius.artist}."
desc_first_embed = f"[Lien vers les paroles sur le site]({song_genius.url})"
type_de_comptage = "\n\n" if paroles.count("\n\n") > 2 else "\n"
for ligne in paroles.split(type_de_comptage):
if len(ligne) >= 2048:
type_de_comptage = "\n"
for ligne in paroles.split(type_de_comptage):
if len(f"{lignetotal}{type_de_comptage}{ligne}") < 1900:
lignetotal = f"{lignetotal}{type_de_comptage}{ligneFormatage(ligne)}"
else:
if premierembed == True:
premierembed = False
embed = discord.Embed(title = title_first_embed, description = f"{desc_first_embed}{lignetotal}", color = couleur_embed)
embed.set_thumbnail(url = song_genius.song_art_image_url)
await message.edit(embed = embed)
else:
embed = discord.Embed(description = lignetotal, color = couleur_embed)
await ctx.send(embed = embed)
lignetotal = f"{ligneFormatage(ligne)}"
temps_requete = int(round(nowCustom() * 1000)) - temps_requete
footer_embed = f"Pour {userOrNick(ctx.author)} par Genius en {round(temps_requete / 1000, 2)} s."
await ctx.message.add_reaction(emoji = '')
if premierembed == True:
premierembed = False
embed = discord.Embed(title = title_first_embed, description = f"{desc_first_embed}{lignetotal}", color = couleur_embed)
embed.set_footer(icon_url = ctx.author.avatar_url, text = footer_embed)
return await message.edit(embed = embed)
else:
embed = discord.Embed(description = lignetotal, color = couleur_embed)
embed.set_footer(icon_url = ctx.author.avatar_url, text = footer_embed)
return await ctx.send(embed = embed)
else:
await ctx.message.add_reaction(emoji = '')
await ctx.send(f"Aucune musique demandé... `{ctx.prefix}lyrics/l/lyrics <song>`.")
@commands.command(name='lyricsromanized', aliases = ['lr', 'lyricromanized'], hidden = True)
async def _lyricsromanized(self, ctx, *, song: str = None):
player: Player = self.bot.wavelink.get_player(guild_id=ctx.guild.id, cls=Player, context=ctx)
if not song and player.is_playing:
song = re.sub(r"(\ )?\(.*\)", "", player.current.title)
await ctx.invoke(self.client.get_command("lyrics"), song = f"{song} romanized" if song else song)