0
votes

I am trying to make my bot's role a pink color for the current server using the Client.on("ready") However whenever I run the bot with what I currently have the console returns:

(node:6504) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined

Here is my code I am currently using. I know I need to for loop on every guild but I'm not sure how I would do that, I could use a for or just a .forEach however I can't find the correct way to do such.

const role = client.guild.roles.cache.get('Aiz Basic+');  
role.edit({ name: 'Aiz Basic+', color: '#FFC0CB' });

In advance thank you to anybody who replies and helps with my message.

2
Does the bot have permission to edit that? Have you tried editing a different role that is below the bot's role in the roles list?dacx
@dacx yes, my bot requires all of the necessary privileges.Eh_asuna
Have you tried editing a different role that is below the bot's role in the roles list?dacx
Yes, I can confirm that I can do that. However the code I am trying to run is happening in the "ready" event. So I can't edit the role because client.guild isn't defined for some reason.Eh_asuna

2 Answers

2
votes

Client#Guild Isn‘t existing. Only Client#Guilds. You can fix this problem by looping through the cache of all your guilds with:

client.guilds.cache.forEach((g) => {  })

And inside the loop you can use your code after fixing the .get function, because the get function needs a role id not a role name. You could find the role via the name by writing:

const role = <Guild>.roles.cache.find((r) => r.name === /* The role name */);

Make sure you replaced <Guild> with your guild variable.

1
votes

Try this code if client.guild is not defined, it could also be because you are using bot instead of client

let role = message.guild.roles.cache.find(role => role.name === "Aiz Basic+");