简体   繁体   中英

Creating a role stops roles.add from working [discord.js]

When using an existing role I can add it to a member but when it is created it is considered an invalid type.

This:

let Role = guild.roles.create({
   data: {
      name: 'role',
      color: 'BLUE',
      permissions: 0,
    },
    reason: 'role',
  })

Delivers:

UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not an Role, Snowflake or Array or Collection of Roles or Snowflakes.

This:

let Role = message.guild.roles.cache.find(role => role.name == 'role');

Delivers:

Command sent: -role by Skuffies
Role Successfully added.

Code used for adding role:

message.mentions.members.first().roles.add(Role);

This is because Guild#roles#create returns a promise, it's an asynchronous function. Whenever you attempt to add the new role it doesn't exist at the time of the execution because the role creation is still under process.

Handling The Promise Using: Promise#then()

let Role = guild.roles.create({
   data: {
      name: 'role',
      color: 'BLUE',
      permissions: 0,
    },
    reason: 'role'
}).then(thisNewRole => {
   const member = message.mentions.members.first();
   member.roles.add(thisNewRole);
})

Handling The Promise Using: Async/Await

  • Ensure you're inside an async function.
async yourFunctuon() {
   let Role = await guild.roles.create({
   data: {
      name: 'role',
      color: 'BLUE',
      permissions: 0,
    },
    reason: 'role',
  })

   const member = message.mentions.members.first();
   member.roles.add(Role);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM