aboutsummaryrefslogtreecommitdiff
path: root/code/cogs/lyrics.py
blob: 8c47457778645e62bcf2bfc2f28cc42efafd32b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import discord
import datetime
from discord import app_commands
from discord.ext import commands
from cogs.music import Music

from utils.config import create_embed


class Lyrics(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @app_commands.command()
    @app_commands.check(Music.create_player)
    async def lyrics(self, interaction: discord.Interaction):
        "Get lyrics for the song that is currently playing"
        player = self.bot.lavalink.player_manager.get(interaction.guild.id)

        # If the Genius API client is not setup, send an error message
        if not self.bot.genius:
            embed = create_embed(
                title="Lyrics Feature Error",
                description=(
                    "The lyrics feature is currently disabled due to errors"
                    " with the Genius API."
                ),
            )
            return await interaction.response.send_message(
                embed=embed, ephemeral=True
            )

        # Defer the interaction to avoid getting 404 Not Found errors
        # if fetching the lyrics takes a long time
        await interaction.response.defer(ephemeral=True)

        # Search for the songs lyrics with Genius
        song = self.bot.genius.search_song(
            player.current.title, player.current.author
        )

        # If no lyrics are found, send an error message
        if song is None:
            embed = create_embed(
                title="Lyrics Not Found",
                description=(
                    "Unfortunately, I wasn't able to find any lyrics for the"
                    " song that is currently playing."
                ),
                thumbnail=player.current.artwork_url,
            )
            return await interaction.edit_original_response(embed=embed)

        # Remove unwanted text
        lyrics = song.lyrics
        lyrics = lyrics.split(" Lyrics", 1)[-1]
        lyrics = lyrics.replace("You might also like", "\n")
        lyrics = lyrics[:-7]

        # If the lyrics are too long, send just a link to the lyrics
        if len(lyrics) > 2048:
            embed = create_embed(
                title=(
                    f"Lyrics for {player.current.title} by"
                    f" {player.current.author}"
                ),
                description=(
                    "The lyrics for this song are too long to display on"
                    " Discord. [Click here to view the lyrics on"
                    f" Genius]({song.url})."
                ),
                thumbnail=player.current.artwork_url,
            )
            return await interaction.edit_original_response(embed=embed)

        # If everything is successful, send the lyrics
        embed = create_embed(
            title=(
                f"Lyrics for {player.current.title} by {player.current.author}"
            ),
            description=f"Provided from [Genius]({song.url})\n\n" + lyrics,
            thumbnail=player.current.artwork_url,
        )
        await interaction.edit_original_response(embed=embed)


async def setup(bot):
    await bot.add_cog(Lyrics(bot))