简体   繁体   English

创建角色会使角色停止工作 [discord.js]

[英]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.这是因为Guild#roles#create返回一个 promise,它是一个异步 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()使用: Promise#then()处理 Promise

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处理 Promise 使用:异步/等待

  • Ensure you're inside an async function.确保您在异步 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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