0

I'm trying to add a role to someone who sends a message. I've tried this but it comes up with an error

I've tried:

message.guild.createRole({name:"RoleName", color: "#ff0000"})
var memberRole = message.guild.roles.find(role => role.name === "RoleName");
message.member.addRole(memberRole);

if you have any questions about the topic feel free to ask

  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – slothiful Jan 16 '20 at 15:54

1 Answers1

1

I believe Guild#createRole() returns a Promise for a Role, resolving asynchronously. Therefore, if you create the role in line 1 and you then try to find the role synchronously in line 2, it won’t be under Guild#roles (yet).

Instead, wait for the Promise to resolve. You can then pass the returned Role directly to the GuildMember#addRole() method.

message.guild.createRole({name:"RoleName", color: "#ff0000"})
    .then(createdRole => message.member.addRole(createdRole));
Cloud
  • 674
  • 1
  • 3
  • 17