[英]Is there a way to add a role to a member as soon as the role is created? (Discord.js v12)
I was trying to create a new role and add it to a member when running a command once, but it seems that I have to run the command twice (once: for the role to get created, twice: for the role to get added to the member).我试图在运行一次命令时创建一个新角色并将其添加到成员中,但似乎我必须运行该命令两次(一次:用于创建角色,两次:用于将角色添加到会员)。
I guess this would be due to this error: TypeError [INVALID_TYPE]: Supplied roles is not an Role, Snowflake or Array or Collection of Roles or Snowflakes.
我猜这可能是由于这个错误: TypeError [INVALID_TYPE]: Supplied roles is not an Role, Snowflake or Array or Collection of Roles or Snowflakes.
if (command === 'test') {
if (!message.mentions.users.size) {
return message.reply('You need to tag a user!');
}
const member = message.mentions.members.first();
const testRole = message.guild.roles.cache.find(role => role.name === 'TestRole');
if (!testRole) {
message.guild.roles.create ({
data: {
name: 'TestRole',
color: 'RANDOM',
},
}).catch(() => {
message.reply('Unable to create role');
});
}
member.roles.add(testRole).catch((error) => {console.log(error);});
}
Would there be a workaround for this in order to add the role to the member as soon as it is created?是否有解决方法以便在创建成员后立即将角色添加到成员?
Since roles.create
returns a promise, you can use .then()
to add the role to the member由于roles.create
返回 promise,您可以使用 .then .then()
将角色添加到成员
if (command === 'test') {
if (!message.mentions.users.size) {
return message.reply('You need to tag a user!');
}
const member = message.mentions.members.first();
const testRole = message.guild.roles.cache.find(role => role.name === 'TestRole');
if (!testRole) {
message.guild.roles.create ({
data: {
name: 'TestRole',
color: 'RANDOM',
},
})
.then((role) => {
member.roles.add(testRole)
})
.catch(() => {
message.reply('Unable to create role');
});
} else {
member.roles.add(testRole).catch((error) => {console.log(error);});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.