Displaying all connected users Discord.js

浪尽此生 提交于 2019-12-07 14:37:28

问题


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();

回答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.




回答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.




回答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);});



回答4:


Hey you better try this

code

    var guilds = client.guilds;
    console.log(guilds);

this returns a collecting which contain all the guilds which bot is connected and the guild contains its all members it gives lot of info see the cmd log

more refer here

what is the hell is guild use this link

https://discord.js.org/#/docs/main/stable/class/Guild

what the hell is collection look down

https://discord.js.org/#/docs/main/stable/class/Collection



来源:https://stackoverflow.com/questions/43125985/displaying-all-connected-users-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!