Reputation: 35
I want to get members count in discord bot but it gives error. I search for it through internet for this and i don't find it! Here is code:
const Commando = require('discord.js-commando');
const bot = new Commando.Client({commandPrefix: '$'});
const TOKEN = 'here is token';
const MIN_INTERVAL = 3 * 1000;
const guild = bot.guilds.get("394805546450026496");
bot.registry.registerGroup('connectc', 'Connectc');
bot.registry.registerGroup('defaultc', 'Defaultc');
bot.registry.registerDefaults();
bot.registry.registerCommandsIn(__dirname + "/commands")
bot.on('ready', function(){
console.log("Ready");
setInterval(function(){
var memberCount = guild.members.filter(member => !member.user.bot).size;
var memberCountChannel = bot.channels.get("547805078787194891");
memberCountChannel.setName("👤Osoby: "+ memberCount +" 👤");
}, MIN_INTERVAL);
});
bot.login(TOKEN);
And here error:
C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:18
var memberCount = guild.members.filter(member => !member.user.bot).size;
^
TypeError: Cannot read property 'members' of undefined
at CommandoClient.<anonymous> (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:18:29)
at CommandoClient.emit (events.js:194:15)
at WebSocketConnection.triggerReady (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:125:17)
at WebSocketConnection.checkIfReady (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:141:61)
at GuildCreateHandler.handle (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\discord.js\src\client\websocket\packets\handlers\GuildCreate.js:13:31)
at WebSocket.onMessage (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:189:13)
Please help me!
Upvotes: 1
Views: 47317
Reputation: 1
//DISCORD.JS V13 MEMBERCOUNT COMMAND;
const Discord = require("discord.js")
let guild = message.guild
let users = guild.members.cache.filter((m) => !m.user.bot)).size;
let bots = guild.members.cache.filter((m) => m.uxer.bot).size;
let memberCount = new Discord.MessageEmbed()
.setTitle(`MemberCount Information of ${guild.name}`)
.addField("Humans", `${users}`)
.addField("Bots", `${bots}`)
.setColor("RANDOM")
.setFooter({
text: `${guild.name},
iconURL: guild.iconURL()
})
message.channel.sendTyping();
message.reply({
embeds : [memberCount]
});
Upvotes: -1
Reputation: 1
let memberChannel = message.guild.channels.cache.get("802617217450901514");
memberChannel.setName("Member Count: " + message.guild.memberCount);
Upvotes: 0
Reputation: 21
// update member
let myGuild = client.guilds.get("guilds_id_here");
let memberCount = myGuild.memberCount;
let memberCountChannel = myGuild.channels.get("channel_id_here");
memberCountChannel.setName("Member•" +memberCount+ "•User")
Upvotes: 2
Reputation: 6625
From your error results that the guild is undefined. I ran the code and it's working as expected.
module.exports.run = async (client, message, arguments) => {
const guild = client.guilds.get("566596189827629066");
setInterval(function () {
var memberCount = guild.members.filter(member => !member.user.bot).size;
var memberCountChannel = client.channels.get("626462657817477131");
memberCountChannel.setName(`${guild.name} has ${memberCount} members!`);
}, 1000);
};
Please, double-check that 394805546450026496 is a valid guild-id and not a channel-id/user-id. If it is, check if the bot is in the mentioned guild.
Another thing, it's recommended to see if a guild is available before performing operations or reading data from it. You can check this with guild.available.
Upvotes: 4
Reputation: 321
It looks like when the function guild.members.filter(member => !member.user.bot).size;
is called the value of guild is undefined. Are you sure const guild = bot.guilds.get("394805546450026496");
is returning the guild correctly? Can you try logging the value of guild
after the get is called? Im not seeing the get(String id)
method anywhere in the discord.js documentation. Have you tried using the recommended find()
?
Try this instead.
const guild = bot.guilds.find(guild => {guild.id == "394805546450026496"});
Upvotes: 0