0

I've made a bot that can play audio through discord but what i want to do now is to make him to say the url in the chat.

@bot.command(pass_context = True)
async def play(ctx,*,query : str):


    opts = {'default_search': 'auto','quiet': True,}    #options pour youtube-dl que je comprends pas
    if voice == None:   #si le bot n'a pas encore été connecté à un channel
        await bot.say('Summon me in your channel first (!summon)')

    elif query[:3] == 'url':
        try:
            n = int(query[4])       
            await bot.say(videos[n][0])
        except Exception as e:
            await bot.say (e)


    elif len(query) != 1: #si le joueur essaie de taper !play recherche et non !play 1/2/3/4
        videos = yt.recherche(query,4)  #on charge les informations des vidéos avec le module yt
        for i in range(4): #on affiche les 4 résultas avec un emebed contenant un apercu de chaque vidéo
            em = discord.Embed(title=videos[i][1], colour=0xff0000, type = 'rich')
            em.set_thumbnail(url='https://i.ytimg.com/vi/'+videos[i][0][32:]+'/hqdefault.jpg?sqp=-oaymwEXCNACELwBSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLDBtpHoodvOvDCPjzg9t7PzSljI3A')
            await bot.send_message(ctx.message.channel,None,embed=em)
        await bot.say('Make your choice! (!play 1/2/3/4)')

    else:   #si le joueur essaie de choisir une video avec !play 1/2/3/4
        try:
            if player != None:  #si le bot joue déjà une chanson, on stoppe la précédente avant de commencer la suivante (sinon ca plante)
                player.stop()
            query = int(query)  #on convertit en entier : str -> int
            player = await voice.create_ytdl_player(videos[query-1][0],ytdl_options=opts) #on initialise le player audio dans la varialble globale
            player.volume = 0.2     #on fixe le volume
            player.start()          #on démarre la lecture

        except Exception as e:     #exception atteinte en général si on a pas réussi à faire query = int(query), c'est à dire que le joueur à fait une faute de frappe
            await bot.say(e)

my problem is when i want to change the elif query:3 to something else to shorter the "url" i wanted to put like .play -u .... but i can't make it work

import requests  #librairie de base permettant d'ouvrir une page web dans la console en gros

def recherche(query,nb):

query = query.replace(' ','+')
url = 'https://www.youtube.com/results?search_query='+query
r = requests.get(url).text

balise = 'data-context-item-id="'   #balise signalant l'élément manquant dans le code source

liste_videos = []

for e in range(nb): #on ajoute les url de chaque video

    liste_videos.append([])
    i = r.index(balise)     #renvoie la position de la balise dans le code
    liste_videos[-1].append('https://www.youtube.com/watch?v='+r[i+22:i+22+11])
    r = r[i+100:]   #on coupe le début du code pour aller chercher dans la suite plus facilement


for vid in range(nb): #puis leurs titre

    url = liste_videos[vid][0]  #on prend l'url d'une video
    r = requests.get(url).text  #on ouvre la page correspondante
    title = r[r.index('<title>')+7:r.index('</title>')]     #et on cherche le titre dedans
    liste_videos[vid].append(title) #puis on met le titre dans le tableau video
    u = liste_videos[vid][0]  

return liste_videos     #on finit par renvoyer ce tableau
Basho
  • 47
  • 11

1 Answers1

0

hacky: suppose you call the bot with "play u1"

 elif query[0] == 'u':
    try:
        n = int(query[1])  
        await bot.say(videos[n][0])

essentially query is string which can be accesd like a list. Have a look here:

How to get char from string by index?

tiennes
  • 79
  • 5
  • thank you for your answer! but if I change it like this the bot will think of it as a new query and thus just spit out 4 videos about u 1 – Basho May 26 '18 at 22:21
  • I think there are 2 different concerns. 1) parse the u option, and see if you land there with `await bot.say('I am here')` 2) It depends what is in `videos` and where the url is located there – tiennes May 26 '18 at 23:13
  • sorry, i don't get what you mean by parse the u option? And I've added the bit of the code where videos come from! thanks for the help it's really appreciated – Basho May 26 '18 at 23:41
  • You should probably make a seprate command: https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html. Itreally depends on how you call your methods, in addition you cannot change url to since recherche expects the url string to query youtube, also videos is defined after elif == url, which will break. – tiennes May 27 '18 at 01:15
  • This is where is it weird if I keep elif query :3 and URL the command works its when I change those that it doesn't work anymore. – Basho May 27 '18 at 09:59