Role cache get undefined

const guild = client.guilds.cache.get('929288640142663720');
let roles = [];
if (config.admin_role) {
config.admin_role.forEach(async (role) => {
console.log(role);
const roleFetched = guild.roles.cache.get(role);
console.log(roleFetched);
if (!roleFetched) return roles.push('[INVALID ROLE]');

roles.push(roleFetched);
await channel.permissionOverwrites.create(roleFetched.id, {
SendMessages: true,
ViewChannel: true,
AttachFiles: true,
AddReactions: true,
EmbedLinks: true,
ReadMessageHistory: true
})
});
} else {
roles.push("No roles were added to config.js file.");
}
const guild = client.guilds.cache.get('929288640142663720');
let roles = [];
if (config.admin_role) {
config.admin_role.forEach(async (role) => {
console.log(role);
const roleFetched = guild.roles.cache.get(role);
console.log(roleFetched);
if (!roleFetched) return roles.push('[INVALID ROLE]');

roles.push(roleFetched);
await channel.permissionOverwrites.create(roleFetched.id, {
SendMessages: true,
ViewChannel: true,
AttachFiles: true,
AddReactions: true,
EmbedLinks: true,
ReadMessageHistory: true
})
});
} else {
roles.push("No roles were added to config.js file.");
}
When i using this it backs undefined how could i get the role
21 Replies
d.js toolkit
d.js toolkit2mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
treble/luna
treble/luna2mo ago
log the id or show your logs and show the full error
Unicorn.
Unicorn.OP2mo ago
no error,and console.log shows undefined
Amgelo
Amgelo2mo ago
you have 2 logs there plus the one you were asked, which one logs undefined
Unicorn.
Unicorn.OP2mo ago
The second which rolefetch log
treble/luna
treble/luna2mo ago
and what does the first one log
Unicorn.
Unicorn.OP2mo ago
Each admin_role snowflake
Amgelo
Amgelo2mo ago
then there's no role with that id make sure the ids in your config are correct and they're strings
Unicorn.
Unicorn.OP2mo ago
yeah i sure its in the guild which have this role wait let me check the guild fetch 929288640142663720
treble/luna
treble/luna2mo ago
okay but what exactly
Unicorn.
Unicorn.OP2mo ago
do u mean config example?
treble/luna
treble/luna2mo ago
...the ones logged in your console
Unicorn.
Unicorn.OP2mo ago
936193745823621120(yellow color) undefined(grey color)
treble/luna
treble/luna2mo ago
log config.admin_role
Unicorn.
Unicorn.OP2mo ago
wait let me open laptop [ 1314547409690300400, 936193745823621100, 1109490292438073500 ]
Mark
Mark2mo ago
those are numbers, not strings. snowflakes are strings snowflakes are larger than the max safe int in js, so they end up getting rounded if you convert them to a number. don't do that you should also do one api request to modify the channel's permission overwrites for all desired roles/users at once, instead of multiple api requests (which might also have unintended results due to race conditions)
Unicorn.
Unicorn.OP2mo ago
:Thinkeng: okay but how to streamline the procedure
Unicorn.
Unicorn.OP2mo ago
using this?
No description
Mark
Mark2mo ago
if it's for a channel you're creating, yes, you can pass all of the desired overwrites during the create request
Unicorn.
Unicorn.OP2mo ago
but it will show such large array list
Mark
Mark2mo ago
That's immaterial, it's just code And one large array for one API request is better than several smaller arrays over multiple requests If the permissions are the same for each role, then you can just define the array as a constant and reference that

Did you find this page helpful?