Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:14:57+00:00 2026-06-09T03:14:57+00:00

I have the following code and am attempting to make a Turntable bot using

  • 0

I have the following code and am attempting to make a Turntable bot using node.js. this piece of code says when a user types “q+” we have to make sure its not already on the queue, that its not already DJing, and if it meets those 2 requirements, add them to the queue. Otherwise if it doesnt meet one of those first 2 criteria, tell the user and do not touch the queue.

My problem is the “isCurrentDJ(userId)”. When I pass a userId through that function, the function gives me the correct answer. However the function ALWAYS passes back “false” even when the answer is “true” and the console.log() function within the isCurrentDJ(userId) function proves so.

I am not the most js-savvy person, so I think this may be a variable scope issue. But I am really not sure and have been struggling with it for hours! Any help would be greatly appreciated. Thanks!

// When someone speaks, listen to see if it is one of the q commands
bot.on('speak', function (data) {
var name = data.name;
var text = data.text;
var userId = data.userid;

// q+ :: Add to Queue
if (text.match(/^q\+$/)) {

  //Index is the position in the queue that this person's name is found.
  //If its not found, -1 is returned.
  var index = queue.indexOf(name);

  //Function to determine if the user is currently a DJ
    function isCurrentDJ(user_id, callback){
      bot.roomInfo(false, function (data) {
        var djList = data.room.metadata.djs;
        for (i = 0; i < djList.length; i++){
          if (djList[i] == user_id){
            console.log('recognized as a DJ'); //Consistently printed!
            callback(true);
          }
        }
        callback(false);
      });
    }


  isCurrentDJ(userId, function(isDJ) {
    //If the user is already in the queue
    if(index > -1){
      //Tell them they are already in there
      bot.speak('You are already on the list');
    } else if(isDJ){
      //Otherwise if they are already a DJ tell them that
      bot.speak('You are already a DJ, '+name);
    }else{
      //Otherise if they are not in the queue add user to end of queue
      queue.push(name);
      //Tell them about it and the updated q
      bot.speak(name+' has been added to queue.');
    }
  });

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:14:59+00:00Added an answer on June 9, 2026 at 3:14 am

    Your problem is that bot.roomInfo is an asynchronous function.

    When you call it, it immediately returns and currDJ is still false. A little while later, the callback (function(data) {...) is called. Most of node.js’s API are async so that your code never blocks.

    Here’s how you should rewrite your code:

    // When someone speaks, listen to see if it is one of the q commands
    bot.on('speak', function (data) {
       var name = data.name;
       var text = data.text;
       var userId = data.userid;
    
       // q+ :: Add to Queue
      if (text.match(/^q\+$/)) {
    
      //Index is the position in the queue that this person's name is found.
      //If its not found, -1 is returned.
      var index = queue.indexOf(name);
    
      //Function to determine if the user is currently a DJ
        function testCurrentDJ(user_id, cb){
    
          bot.roomInfo(false, function (data) {
            var djList = data.room.metadata.djs;
            for (i = 0; i < djList.length; i++){
              if (djList[i] == user_id){
                console.log('recognized as a DJ'); //Consistently printed!
                return cb(true);
              }
            }
    
            cb(false);
          });
        }
    
      //If the user is already in the queue
      if(index > -1){
        //Tell them they are already in there
        bot.speak('You are already on the list');
        return;
      }
    
      testCurrentDJ(userId, function(isDJ) {
          //Otherwise if they are already a DJ tell them that
          if(isDJ) {
            bot.speak('You are already a DJ, '+name);
          } else {
           //Otherise if they are not in the queue add user to end of queue
           queue.push(name);
           //Tell them about it and the updated q
           bot.speak(name+' has been added to queue. Is Current DJ? '+isDJ);
      }
    
        })
    }
    

    I’ve just updated your code to show you the basic idea. In node.js’s API, the first argument of callbacks is usually an error object that is null if everything went fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for inserting data into database using PDO. It inserts data
I have following code using hibernate to throw a custom exception on error and
I have the following code that is attempting to start each of the commands
I have the following piece of code for handling exceptions in my web application:
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for loading image from url in xml parsing endElement method
I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS
I have following code that I am compiling in a .NET 4.0 project namespace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.