Home > Blockchain >  Discord.js v12 server member count
Discord.js v12 server member count

Time:04-17

(Welcome command using canvas)

How could I fetch the server member count as soon as someone joins??

Because I use that line of code

 const guild = client.guilds.cache.get("843190900930510869");
    let image = await welcomeCanvas
      .setUsername(member.user.tag)
      .setDiscriminator(member.user.discriminator)
      .setMemberCount(guild.memberCount) //this line
    etc...

And well, it just doesn't send the image..

Error:

(node:6387) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'memberCount' of undefined at GuildMemberAddListener.exec (/app/listeners/guildMemberAdd.js:100:29) (node:6387) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:6387) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

CodePudding user response:

The event client.on('guildMemberAdd', () => {}) will return a GuildMember object. Simply use this GuildMember object to get the guild they entered GuildMember.guild then check if that guild is available to the client using guild.available. If it is available you can access all the properties on that guild including the guild.memberCount property.

client.on('guildMemberAdd', (member) => {
  const guild = member.guild
  if (!guild.available) return console.error('Uh Oh Stinky...')
  const guildMemberCount = guild.memberCount
  console.log(guildMemberCount)
})
  • Related