Problem with Promises in javascript (discord bot)

0

I'm not getting any way to make a part of the code that works with promises work. In the code, I do a check in arrays (obtained by a json) to know if youtube video is blocked or not, however, I need the rest of the method to wait for that part to finish, which I can not do with the promises. If there is an error (video is blocked), I have to generate a new error so that my method detects this error and sends the message, this method that will check the error and send the message in discord chat, are the methods of " play ", which call a" search "method that calls this my problem method" searchTime ", and doing a try catch, I see if it has an error or not. I need to pass the error to this method of play, because it is it that warns the user of the error when playing "play" the song. You can see in the photo I sent that the bot returns me an "object promise" message instead of the time of the video, because the promise problem is inside this method that takes the time of the video, and then the return is not coming out correctly.

Search methodTime (has to return the time of the video or if the video is blocked, it has to throw a new error for the method that called it (play) catch the error and send the message in the discord chat):

async function pesquisarTime(idp, titlep){
    let linkYoutube = "https://www.googleapis.com/youtube/v3/videos?id=" + idp + "&part=contentDetails&key=" + config.ytapi;
    let response = await fetch(linkYoutube);
    if(response == undefined || response == null){
        throw new Error("Vídeo \"" + titlep + "\" inválido!");
    }
    let info = await response.json();
    let regionRestriction = "";
    let allowed = "";
    let bool;
    let bool2;
    let resposta;
    let resposta2;
    let x = 0;
    let x2 = 0;
    try{
        info.items[0].contentDetails;
    }catch(err){
        throw new Error("Vídeo \"" + titlep + "\" inválido!");
    }
    try{
        regionRestriction = info.items[0].contentDetails.regionRestriction.blocked;
        //console.log("regionRestriction = " + regionRestriction);
    }catch(err){
        
    }
    try{
        allowed = info.items[0].contentDetails.regionRestriction.allowed;
        //console.log("allowed = " + allowed);
    }catch(err){

    }
    if(regionRestriction != undefined && regionRestriction.length > 0){
        bool = new Promise(async function(resolve){
            for(x = 0; x < regionRestriction.length; x++){
                if(regionRestriction[x] == "BR"){
                    x = regionRestriction.length;
                    //console.log("resolve region = true");
                    resolve(true);
                }
                else{
                    if(x == regionRestriction.length - 1){
                        //console.log("resolve region = false");
                        resolve(false);
                    }
                }
            }
        });
        try{
            resposta = await bool;
            next(resposta);
        }catch(err){
            throw err;
        }
        //let procurarErro = await async function(){
            //await bool.then(function(retorno){
                //Promise.resolve();
                //console.log(retorno + " para vídeo bloqueado na sua região");
            //}).catch(function(error){
                //throw new Error(error);
            //});
        //}
        //resposta = await procurarErro();
    }
    if(allowed != undefined && allowed.length > 0){
        bool2 = new Promise(async function(resolve){
            for(x2 = 0; x2 < allowed.length; x2++){
                if(allowed[x2] == "BR"){
                    x2 = allowed.length;
                    //console.log("resolve allowed = false");
                    resolve(false);
                }
                else{
                    if(x2 == allowed.length - 1){
                        //console.log("resolve allowed = true");
                        resolve(true);
                    }
                }
            }
        });
        try{
            resposta2 = await bool2;
            next2(resposta2);
        }catch(err){
            throw err;
        }
        //let procurarErro = await async function(){
            //await bool2.then(function(retorno){
                //Promise.resolve();
                //console.log(retorno + " para vídeo bloqueado na sua região");
            //}).catch(function(error){
                //throw new Error(error);
            //});
        //}
        //resposta2 = await procurarErro();
    }
    function next(result){
        if(result){
            console.log("bool region");
            throw new Error("Vídeo \"" + titlep + "\" bloqueado na sua região!");
        }
    }
    function next2(result){
        if(result){
            console.log("bool2 allowed");
            throw new Error("Vídeo \"" + titlep + "\" Bloqueado!");
        }
    }
    let durationp = info.items[0].contentDetails.duration.replace("PT", "");
    durationp = consertarTempo(durationp);
    durationp = calibrar(durationp);
    return durationp;
}

How is the return of the method in the discord (instead of beginning the time of the song, it printa an [object promise] in place):

Methodplay(methodthatcallsthesearchmethodthatwilllatercallthesearchTimemethod):

async function play(music, boolLinkId){
            let retorno;
            if(boolLinkId == false){
                try{
                    retorno = await pesquisar(music);
                }catch(err){
                    return message.channel.send(err.message);
                }
                retorno = retorno.split("/");
                id = retorno[0];
                nome = retorno[1];
                tempo = retorno[2];
            }
            ...Tem mais código pela frente, mas isso é o que importa
}

Search method (method calling searchTime actually):

async function pesquisar(query){
    
    let linkYoutube = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=1&q=" + query + "&key=" + config.ytapi;
    let response = await fetch(linkYoutube);
    if(response == null || response == ""){
        throw new Error("Vídeo não encontrado!");
    }
    let info = await response.json();
    let idp = info.items[0].id.videoId;
    let titlep = info.items[0].snippet.title;
    let totalResults = info.totalResults;
    let durationp;
    if(totalResults == 0){
        throw new Error("Vídeo \"" + titlep + "\" deletado!");
    }
    try{
        durationp = pesquisarTime(idp, titlep);
    }catch(err){
        throw err;
    }
    return idp + "/" + titlep.replace("/", "{") + "/" + durationp;
}

All help is welcome! Please, I've been trying to make this work for weeks but I can never make this promise work properly!

    
asked by anonymous 11.10.2018 / 06:42

0 answers