Displaying all connected users Discord.js
Displaying all connected users Discord.js
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();
4 Answers
4
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);});
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.
Odd: try just doing
console.log(Bot.users); and see what it says? The docs say All of the User objects that have been cached at any point, mapped by their IDs Type: Collection<Snowflake, User> so I'd think it would be an associative array, but perhaps not. You should also make sure there are some users logged in.– Femi
Mar 30 '17 at 19:13
console.log(Bot.users);
Displays... bot: false, lastMessageID: null }, '229092771175202817' => User { id: '229092771175202817', username: 'Czel', discriminator: '1053', avatar: null, bot: false, lastMessageID: null }, _array: null, _keyArray: null }
– Carl
Mar 30 '17 at 19:23
Yeah, I was right, it is an associative array. Adjusted the answer to print out the id and username.
– Femi
Mar 30 '17 at 19:46
This kicks back.... /index.js:18 if (user instanceof User) { ^ ReferenceError: User is not defined
– Carl
Mar 30 '17 at 19:51
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.
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
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It's kicking back a "_array : null _keyArray : null". I'm not sure what this means
– Carl
Mar 30 '17 at 19:09