MCbabel / discord-music-bot

🎶 A powerful, feature-rich Discord Music Bot built with Python and discord.py, allowing seamless integration with YouTube and Spotify. Created with the assistance of ChatGPT to demonstrate the capabilities of AI-driven development.
Other
2 stars 0 forks source link

youtube word search support (code included) #1

Closed TuteOneDev closed 1 month ago

TuteOneDev commented 1 month ago

Why?

to make the user experience better because it is sometimes annoying to have to use only links.

How?

I designed a quick little patch which uses the youtubesearchpython library and a small function that allows you to search and find by words whatever you want on youtube:

from youtubesearchpython import VideosSearch

def search_youtube_video(query):
    try:
        search = VideosSearch(query, limit=1)
        result = search.result()
        if result['result']:
            video_url = result['result'][0]['link']
            return video_url
        else:
            return None
    except Exception as e:
        return None

then I modified the conditions of the /play command to have a third condition where if it's not a spotify or youtube link, it searches youtube with whatever you type:

if 'spotify.com' in url:
     player = await SpotifySource.from_spotify_url(url, loop=bot.loop)
elif 'youtube.com' in url or 'youtu.be' in url:
     player = await YTDLSource.from_url(url, loop=bot.loop, stream=True)    
else:
    # Patch
    search = search_youtube_video(url)
    if search != None:
        player = await YTDLSource.from_url(search, loop=bot.loop, stream=True)    
    else: 
        embed = Messages.error("Could not be found :(")
        await interaction.followup.send(embed=embed, ephemeral=True)
        return

it works quite well but it's not perfect, it's something I did in a couple of minutes but it's not quite clean, so surely it can be improved to make it cleaner and tidier.

Thank you 🥇

p.s.: I love this music bot because it sounds great, however I found a couple of bugs, if I have time I will fix them and I will make you a pull request :)

MCbabel commented 1 month ago

Hi, thank you for your kind feedback. I will definitely release a new small version soon where I will add the feature!

MCbabel commented 1 month ago

I have just published the update with the new function! Thanks for your idea!