【问题标题】:Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes提供的角色不是角色、雪花或数组或角色或雪花的集合
【发布时间】:2021-01-04 16:16:58
【问题描述】:

我试图让它在discord.js 中创建一个角色并将其提供给用户,但我似乎收到了这个错误:

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

这是我的代码:

let d = msg.guild.roles.create({
 data: {
  name: '{:robot: Bot Developer}',
  color: 'ff00aa',
  permissions: 'ADMINISTRATOR',
 },
});
msg.member.roles.add(d);

【问题讨论】:

  • 尝试做msg.member.roles.add(d.id)
  • 问题是您在角色完全创建之前添加了它。使用async msg.guild.roles.create(...) 或使用.then() 语句
  • 这些都不起作用@OctagonalT

标签: javascript node.js discord discord.js


【解决方案1】:

根据 Discord.js 文档,RoleManager.create() 返回一个带有角色的 Promise。

由于您没有await.then,因此d 可能是Promise { <pending> }。 假设你在异步函数中运行,下面的代码应该可以工作。

let d = await msg.guild.roles.create({
 data: {
  name: '{:robot: Bot Developer}',
  color: 'ff00aa',
  permissions: 'ADMINISTRATOR',
 },
});
msg.member.roles.add(d);

如果您处于无法使用异步函数的情况,以下是一种替代方法

msg.guild.roles.create({
 data: {
  name: '{:robot: Bot Developer}',
  color: 'ff00aa',
  permissions: 'ADMINISTRATOR',
 },
}).then(d => {
 msg.member.roles.add(d);
 // Continue on with what you want to do.
})

如果您遇到包含 await is only valid in an async function 的错误,请通过更改以下内容确保您的函数是异步的:

function foo() {
OR (depending on your code)
client.on('message', message =>

async function foo() {
OR (depending on your code)
client.on('message', async message =>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 2020-05-18
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多