Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.94k stars 3.77k forks source link

Running on_message inside command #1558

Closed henriqueosilva closed 6 years ago

henriqueosilva commented 6 years ago

Hello, I've been working on creating a bot for discord but i've been strugling with the play function to it, I wanted to make it work by a string input, to make a search on youtube and present to the user the first 5 results, insted of inputing a link, it's going quite well, but I need a way to run a on_message event inside this play function so that I can get the user input for one of the 5 presented results

These are the two functions in question


    @commands.command()
    async def play(self, *args):
        # url for youtube search
        url = "https://www.youtube.com/results?search_query=" + '+'.join(args)

        data = requests.get(url)
        data = data.text
        soup = BeautifulSoup(data, "html.parser")
        # Returns all the videos in the search
        vids = soup.find_all(attrs={'class': 'yt-uix-tile-link'})
        # Returns the leght of the videos
        lenght = soup.find_all(attrs={'class': 'accessible-description'})
        # Zips the first 5 results
        list = zip(vids[:5], lenght[:5])

        for vids, lenght in list:
            string = vids['title'] + ' - ' + lenght.getText()[3:]
            if string[-1] == '.':
                string = string[:-1]
            await self.bot.say(string)

        await self.bot.say("Insert a number from 1 to 5")
        #Need help from here on
        num = await self.bot.on_message()
        music = vids[num]['href']
        print(music)

    @asyncio.coroutine
    async def on_message(self, message):

        num = int(message.content)
        num = num-1
        if num > 4:
            await self.bot.say("Invalid")
        else:
            return num
jwshields commented 6 years ago

Check out the documentation for the library; There's a function which does exactly what you need here, and it's documented in good detail. https://discordpy.readthedocs.io/en/async/api.html#discord.Client.wait_for_message

Edit: to clarify, questions like this would be better suited for the discord.py server. Invite The issue tracker on github is more for issues/bugs with the library

henriqueosilva commented 6 years ago

Thanks a lot!, I've checked there but must have not seen it. Thanks again and have a good day.