Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
How to get the value of a variable out of the scope of the arrow function which created it

So, i have this code:

client.on("messageCreate", async (msg) => {
  if (msg.content.startsWith("-p")) {
    if (!msg.member.voice?.channel) return msg.channel.send("Por favor...");
    const connection = joinVoiceChannel({
      channelId: msg.member.voice.channel.id,
      guildId: msg.guild.id,
      adapterCreator: msg.guild.voiceAdapterCreator,
    });
    google
      .youtube("v3")
      .search.list({
        key: process.env.YOUTUBE_TOKEN,
        part: "id",
        q: msg.content.slice(3),
        maxResults: 1,
      })
      .then((response) => {
        const { data } = response;
        let link = "https://www.youtube.com/watch?v=" + data.items.id.videoId;
      });
    let args = msg.content.slice(3);
    let stream = await play.stream(args);
    let resource = createAudioResource(stream.stream, {
      inputType: stream.type,
    });
    let player = createAudioPlayer({
      behaviors: {
        noSubscriber: NoSubscriberBehavior.Play,
      },
    });
    player.play(resource);
    connection.subscribe(player);
  }
});

And I would like to use the value of the variable "link", which is a string, as a parameter for the method play.stre(args)

I'm don't fully understand how to get that done

Also, if possible I would like to find a way to store that value too inside an array outside the whole:

*client.on('messageCreate' , async msg => {...   ...})*
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You most likely want to call play.stream from within the then callback of list:

google.youtube("v3")
  .search.list({
    key: process.env.YOUTUBE_TOKEN,
    part: "id",
    q: msg.content.slice(3),
    maxResults: 1,
  })
  .then(async (response) => {
    const { data } = response;
    let link = "https://www.youtube.com/watch?v=" + data.items.id.videoId;
    let stream = await play.stream(link);
  });

Like this, you'll make sure that you actually have a value set for link when using it to call stream.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!