Code for a slash command '/users-with-role' that sends a message to the channel with the number of users with a specific role. Your Discord bot requires the 'Privileged Intents' permission to function.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const role_id = process.env.ROLE_ID
const guild_id = context.params.event.guild_id
// Get all members then filter down to those with the given role ID
// Note: Only works for up to 1000 members
const members = await lib.discord.guilds['@0.1.0'].members.list({ guild_id, limit: 1000 });
const membersWithRole = members.filter(m => !!m.roles.find(r => r === role_id));
const membersWithRoleCount = membersWithRole.length;
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `There are ${members.length} users and ${membersWithRole.length} users with the role <@&${role_id}>`,
});