Fetching Roles

Hey , im trying to fetch all roles in discord with a specific name , hmmi guess im kinda doing right but i cant undrestand the result
16 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Violet
VioletOP2y ago
sry cant upload a photo -_-
const AllList = interaction.guild.roles.cache
.sort((a, b) => b.position a.position)
.map((r) => r.name.includes('Badge'))
.join(' , ');
console.log(AllList);
const AllList = interaction.guild.roles.cache
.sort((a, b) => b.position a.position)
.map((r) => r.name.includes('Badge'))
.join(' , ');
console.log(AllList);
the result is smth like this
false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , false , true , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false
false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , true , false , true , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false , false
so i was doing right most of the part/
Violet
VioletOP2y ago
Violet
VioletOP2y ago
anyone know why these lines of code is using 100% of my server cpu and it takes like 15 second to execute the command ive used the same code for channels ( to find channels with a specific name ) and its working fine. but this one is overusing my cpu
Not So Marv
Not So Marv2y ago
How many roles do you have versus channels?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Violet
VioletOP2y ago
Yeah guilds intent is enabled. Owky dowky ill try it hey , i changed fetch to cache the command is working the same like before ( it still replies after like 15 seconds and it uses 100% cpu )
Violet
VioletOP2y ago
Violet
VioletOP2y ago
we have a lot of channels and a lot of roles honestly
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2y ago
To share long code snippets use a service like gist, sourcebin, starbin, or similar instead of posting them as large code blocks.
Violet
VioletOP2y ago
https://sourceb.in/jpxNE67qR2 here u go 🙂 the part which i use a "user" to see a specific user followers works perfectly fast only the part where its without the "user" and its supposed to show like a leaderboard is going slow
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Violet
VioletOP2y ago
its weird actually since ive sone some similar thing like this but for the text chaannels and it works super fast without using 100% cpu im changing 1 line of it , idk i cant think of anything actually it might work lol nope hey i used this line of code
console.log('Caching');
const Roles = interaction.guild.roles.cache.filter((r) =>
r.name.includes('Badge')
);
console.log('Sorting');
const AllList = Roles.sort((a, b) => b.members.size - a.members.size);
console.log('Saving Followers');
const Followers = AllList.map(
(role) =>
`\n${role.name.slice(0, role.name.indexOf(' '))} : ${
role.members.size
}`
).slice(0, 10);
console.log('Sending Interaction');
Embed.setColor('RANDOM');
Embed.setTitle('TOP Followed Organizers');
Embed.setDescription(`**${Followers}**`);
interaction.followUp({ embeds: [Embed] });
console.log('Caching');
const Roles = interaction.guild.roles.cache.filter((r) =>
r.name.includes('Badge')
);
console.log('Sorting');
const AllList = Roles.sort((a, b) => b.members.size - a.members.size);
console.log('Saving Followers');
const Followers = AllList.map(
(role) =>
`\n${role.name.slice(0, role.name.indexOf(' '))} : ${
role.members.size
}`
).slice(0, 10);
console.log('Sending Interaction');
Embed.setColor('RANDOM');
Embed.setTitle('TOP Followed Organizers');
Embed.setDescription(`**${Followers}**`);
interaction.followUp({ embeds: [Embed] });
so before each line of doing anything i have a log wanted to check where it stops actually and takes time its sorting part
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Violet
VioletOP2y ago
sadly its the same :(( ok i managed to change some part and make the code to execute in 3 secon it still goes to 100% cpu but like for very shorter time
Want results from more Discord servers?
Add your server