From cae2f4444c2b4dca4256bf72f130cb12cda1dcb2 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 10 Aug 2021 10:42:58 +0200 Subject: [PATCH] Adding lyrics --- src/cogs/music.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 11fc3f0..4431a24 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -27,7 +27,14 @@ import re import typing import wavelink 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): client.add_cog(Music(client)) @@ -750,3 +757,71 @@ class Music(commands.Cog, wavelink.WavelinkMixin): else: player.dj = m 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 `.") + + @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)