Client.roles.cache.size => undefined | Discord.js v12

Tags: , ,



client.roles.cache.size doesn’t seem to work for me in v12, just says that cache is undefined

Any alternatives for how I can get the total number of roles the bot is currently instantiated with across all guilds?

Answer

Client has no property called roles. This property belongs to Guild.


You can map your Guilds by roles.cache.size and get the sum of the array.

const roles = client.guilds.cache.map(guild => guild.roles.cache.size);
console.log(`Total Roles: ${roles.reduce((a, b) => a + b, 0)}`);


Source: stackoverflow