Displaying all connected users Discord.js

16,924

Solution 1

Looking at https://discord.js.org/#/docs/main/master/class/Client?scrollTo=users, users is an associative array, not a function. Try:

Bot.on('ready', () => {
    setInterval (function (){
        var u, user;
        for(u in Bot.users){
           user = Bot.users[u];
           if(user instanceof Discord.User) console.log("["+u+"] "+user.username);
        }
    }, 10000);
});

EDIT: should now print out username and user id.

Solution 2

Bot.on("ready", function(){
    var Count;
    for(Count in Bot.users.array()){
       var User = Bot.users.array()[Count];
       console.log(User.username);
    }

})

I'm still quite new to javascript, and I've also had the same problem, so I tried solving it, took me forever to figure this out but I did it anyway, hope this helps!

I didn't add the setInterval thingy but you can add it in if you want. Basically i just added a .array and it worked.

Solution 3

It's going to display all users' username connected to your server, online or not :

Bot.on('ready', () => {
   setInterval (function (){
      for (user of Bot.users){
        console.log(user[1].username);
      }       
   }, 10000);});
Share:
16,924
Carl
Author by

Carl

Updated on June 28, 2022

Comments

  • Carl
    Carl almost 2 years

    I'm trying to create a timer that polls all connected users with discord.js.

    My current code is...

    Bot.on('ready', () => {
        setInterval (function (){
            var u = Bot.users();
            console.log(u);
        }, 10000);
    });
    

    However, it doesn't work with a error "TypeError: Bot.users is not a function".

    I'm just not sure how this works. I've also tried...

    Bot.server.users();
    Bot.guilds.users();