I’m working on a Discord music bot using Python that can play audio from YouTube videos. The bot works fine for playing music, but I want to add a feature where it posts the YouTube URL in the chat when requested.
Here’s my current music command code:
@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 (!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_helper.find_videos(search_term, 4)
for j in range(4):
embed = discord.Embed(title=song_list[j][1], colour=0x0099ff)
embed.set_thumbnail(url=f'https://i.ytimg.com/vi/{song_list[j][0][32:]}/hqdefault.jpg')
await ctx.send(embed=embed)
await ctx.send('Pick a song! (!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 command prefix. Right now I use ‘url’ but I want to make it shorter like ‘-u’ or something similar. When I try to change query[:3] == 'url' to a different pattern, it doesn’t work properly. How can I modify this to use a shorter command like .music -u 1 instead of .music url 1?