I created a Discord music bot in Python that can stream audio from YouTube videos. Everything works fine for playing music, but I’m struggling with one feature. I want the bot to post the YouTube URL in the chat when requested.
@client.command()
async def music(ctx, *, search_term: str):
options = {'default_search': 'auto', 'quiet': True}
if voice_client == None:
await ctx.send('Connect me to a voice channel first using !join')
elif search_term[:3] == 'url':
try:
index = int(search_term[4])
await ctx.send(song_list[index][0])
except Exception as error:
await ctx.send(error)
elif len(search_term) != 1:
song_list = youtube_search.find_videos(search_term, 4)
for x in range(4):
embed = discord.Embed(title=song_list[x][1], color=0x00ff00)
embed.set_image(url=f'https://i.ytimg.com/vi/{song_list[x][0][32:]}/maxresdefault.jpg')
await ctx.send(embed=embed)
await ctx.send('Pick a song (type !music 1/2/3/4)')
else:
try:
if audio_player != None:
audio_player.stop()
choice = int(search_term)
audio_player = await voice_client.create_ytdl_player(song_list[choice-1][0], ytdl_options=options)
audio_player.volume = 0.3
audio_player.start()
except Exception as error:
await ctx.send(error)
My issue is with the URL command part. Right now I use ‘url’ but I want to make it shorter like ‘-u’ or something similar. When I try to change elif search_term[:3] == 'url' to use a different prefix, it doesn’t work properly. How can I modify this to accept shorter commands for displaying URLs?