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();
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();
Share
Improve this question
edited May 4, 2018 at 23:43
user9682247
asked Mar 30, 2017 at 18:42
CarlCarl
2453 gold badges4 silver badges15 bronze badges
4 Answers
Reset to default 1Looking at https://discord.js/#/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.
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.
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);});
You should try this:
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 information below:
What is guild, refer to this link: https://discord.js/#/docs/main/stable/class/Guild
What is collection, refer to this other link: https://discord.js/#/docs/main/stable/class/Collection