Reputation: 69
OK, So I understand that I may need to add an if(!filter) type command into the following, but I'm not sure where or how.
The code file is:
const Discord = require('discord.js');
const config = require('../settings/config.js');
const client = require('../index.js').client;
const Guild_ID = require('../settings/configuration').BOT_SETTINGS;
const totalUsers = client.channels.fetch('859879787614437427');
const onlineUsers = client.channels.fetch('859879824403202059');
const onlineStaff = client.channels.fetch('859879875531374672');
client.guilds.fetch('802700035544317972')
.then(guild => console.log(guild.name))
.catch(console.error);
setInterval(function() {
console.log('Getting stats update..')
var userCount = client.memberCount;
var onlineCount = client.members.filter(m => m.presence.status === 'online').size
var coderCount = client.roles.get('802711739006582846').members.size;
console.log("Total Users: " + userCount);
console.log("Online Users: " + onlineCount);
console.log("Online Staff: " + coderCount);
totalUsers.setName("Total Users: " + userCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
onlineUsers.setName("Online Users: " + onlineCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
codeMonkeys.setName("Online Staff: " + coderCount)
.then(newChannel => console.log(`Stat channel renamed to: ${newChannel.name}`))
.catch(console.error);
}, 30000)
;
My error message is:
Getting stats update..
[Error] An exception happened in process:
TypeError: Cannot read property 'filter' of undefined
at Timeout._onTimeout (/home/container/events/Stats.js:17:36)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
I have half an idea of the fix, but not sure of the actual execution therof. Any assistance appreciated.
Upvotes: 0
Views: 971
Reputation: 1565
Discord.js, as of version 12.0.0, only provides a premade property of client.members
for members who are cached by Discord. Users who are not cached would have to be fetched and resolved as the fetch()
method returns a promise.
As for your question, this property is accessible using its key cache
.
var onlineCount = client.members.cache.filter(m => m.presence.status === 'online').size
As a side note, please train yourself to use let
and const
when var
is not necessary.
Upvotes: 1
Reputation: 1664
Read the error carefully, it's not if(!filter)
you need to check. It's something that you're trying to filter that is undefined
.
In your case this is the culprit line:
var onlineCount = client.members.filter(m => m.presence.status === 'online').size
It seems that the client.members
is undefined at this point in time.
A simple solution is to check if members exist before filtering:
if(client.members !== undefined) {
// I'm also guessing this is an array, the right property to get array size is .length
var onlineCount = client.members.filter(m => m.presence.status === 'online').length
}
I've took a quick look at discord.js
API(assuming you're using it), and I couldn't find any members
property on client
object. Are you trying to get the members of a guild maybe?
Upvotes: 0