Shows a graph of how many users in your server have each specified role, and displays a graph and color key in an embed. You must set up a slash command called /gamegraph and have privileged intents enabled for your bot!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
//role id's to display on the graph
const cod_id = '846031019526193162'
const lol_id = '493450244810604555'
const ow_id = '493450127990587392'
const rl_id = '493450556308717571'
const sb_id = '493450614781771777'
const val_id = '735920950645883003'
let totalPlayers = 0;
const guild_id = `${context.params.event.guild_id}`
let members = await lib.discord.guilds['@0.1.0'].members.list({ guild_id, limit: 1000 });
// Get all members then filter down to those with the given role ID
// Note: Only works for up to 1000 members
function roleCount(role_id)
{
let membersWithRoleCount = 0;
let membersWithRole = members.filter(m => !!m.roles.find(r => r === role_id));
totalPlayers += membersWithRole.length;
membersWithRoleCount = membersWithRole.length;
return `${membersWithRoleCount}`
}
let gameCount = [roleCount(`${cod_id}`), roleCount(`${lol_id}`), roleCount(`${ow_id}`),
roleCount(`${rl_id}`), roleCount(`${sb_id}`), roleCount(`${val_id}`), ];
let gamePercents = []; //[3, 24, 19, 17, 20, 17] should total 100 but sometimes 99 due to rounding
let coloredSquares = [':brown_square:',':green_square:',':orange_square:',
':blue_square:',':purple_square:',':red_square:'];
let printString = [];
let value = [];
for (let i = 0; i < gameCount.length; i++)
{
gamePercents[i] = `${Math.round(gameCount[i]/totalPlayers*100)}`;
console.log(`${gamePercents[i]}`);
value[i] = `${gameCount[i]}`+ " ----- " + gamePercents[i] + "%"
}
j = 0 //sequential position of grid 0-99
counter = 0 //0-->5, changes the sequence's color
num = 0 //also sequential position of grid 0-99 but doesn't affect j loop
for (let j = 0; j < gamePercents.length; j++)
{
for (let k = 0; k < gamePercents[counter]; k++)
{
if (num % 11 != 0) //11 because its 10x10 grid plus newline
{
printString[num] = coloredSquares[counter]
num++
}
else
{
printString[num] = ('\n')
num++
printString[num] = coloredSquares[counter]
num++
}
}
counter++
}
//remove commas from Discord message
printString = printString.join('');
await lib.discord.channels['@0.1.2'].messages.create
({
channel_id: `${context.params.event.channel_id}`,
content: `${printString}`,
embeds:
[
{
description: '**Color Key**',
color: 0x33669a,
fields:
[
{
name: 'Call of Duty',
value: ':brown_square:',
inline: true
},
{
name: 'League of Legends',
value: ':green_square:',
inline: true
},
{
name: 'Overwatch',
value: ':orange_square:',
inline: true
},
{
name: 'Rocket League',
value: ':blue_square:',
inline: true
},
{
name: 'Smash Bros',
value: ':purple_square:',
inline: true
},
{
name: 'Valorant',
value: ':red_square:',
inline: true
}
]
}
]
});