55import requests
66import textwrap
77
8-
98class MusicCommands (app_commands .Group ):
109 @app_commands .command (name = "play" , description = "Play a song" )
1110 async def play (self , interaction : discord .Interaction ):
@@ -16,22 +15,26 @@ async def play(self, interaction: discord.Interaction):
1615
1716 @app_commands .command (name = "lyrics" , description = "Get the lyrics of a song" )
1817 async def lyrics (self , interaction : discord .Interaction , search : str ):
19- r = requests .get ("https://some-random-api.com/lyrics?title=" + urllib .parse .quote (search ))
18+ # Defer at the start to avoid timeout because of slow API response
19+ await interaction .response .defer (ephemeral = True , thinking = True )
20+ r = requests .get ("https://api.some-random-api.com/lyrics?title=" + urllib .parse .quote (search ))
2021 try :
2122 song_lyrics = r .json ()['lyrics' ]
22- song_author = r .json ()['author ' ]
23+ song_artist = r .json ()['artist ' ]
2324 song_title = r .json ()['title' ]
24- song_thumbnail = r .json ()['thumbnail' ]['genius' ]
25- song_link = r .json ()['links' ]['genius' ]
25+ song_thumbnail = r .json ()['thumbnail' ]
26+ song_link = r .json ()['url' ]
27+
2628 for chunk in textwrap .wrap (song_lyrics , 4096 , replace_whitespace = False ):
27- embed = discord .Embed (title = song_title + " by " + song_author , description = chunk ,
29+ embed = discord .Embed (title = song_title + " by " + song_artist , description = chunk ,
2830 colour = discord .Colour (0x00ff00 ), url = song_link )
2931 embed .set_thumbnail (url = song_thumbnail )
30- await interaction .response .send_message (embed = embed )
32+ await interaction .followup .send (embed = embed , ephemeral = False )
33+
3134 except KeyError or discord .errors .NotFound :
3235 embed = discord .Embed (title = ":x: Error" , colour = discord .Colour (0xff0000 ),
3336 description = f"Could not find lyrics for `{ search } `!" )
34- await interaction .response . send_message (embed = embed , ephemeral = True )
37+ await interaction .followup . send (embed = embed , ephemeral = True )
3538
3639
3740async def setup (bot ):
0 commit comments