[英]How can I choose a random role and give it to a user Discord.js
Right now my code chooses a random role with Math.Random
and can display it in the console but I don't know how to add that random role to a user.现在我的代码使用Math.Random
选择一个随机角色,并可以在控制台中显示它,但我不知道如何将该随机角色添加给用户。
const random = Math.floor(Math.random() * Choices.length);
console.log(Choices[random])
That's the code that chooses the role and displays it in the console, Choices are the different roles to select from.这是选择角色并在控制台中显示的代码,Choices 是 select 的不同角色。
await reaction.message.guild.members.cache.get(user.id).roles.add();
If I add the constant random
, as the role to add it doesn't detect it as a role.如果我添加常量random
,作为添加它的角色不会将其检测为角色。 Please help, thanks.请帮忙,谢谢。
This is the code这是代码
const GRole = message.guild.roles.cache.find(role => role.name === "Green");
const BRole = message.guild.roles.cache.find(role => role.name === "Blue");
const RRole = message.guild.roles.cache.find(role => role.name === "Red");
const YRole = message.guild.roles.cache.find(role => role.name === "Yellow");
const RandomEmoji = '🔵';
const Choices = [
"GRole",
"BRole",
"RRole",
"YRole",
];
const rereg = (args[0] === "reregister");
if (!rereg) {
let embed = new Discord.MessageEmbed()
.setColor('#cf44c9')
.setTitle('Random Role⠀⠀')
let messageEmbed = await message.channel.send(embed);
messageEmbed.react(RandomEmoji);
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
if (reaction.emoji.name === RandomEmoji) {
const random = Math.floor(Math.random() * Choices.length);
console.log(Choices[random])
await reaction.message.guild.members.cache.get(user.id).roles.add();
}
} else {
return;
}
});
}
in order to assign a role to a user you can use the following code:为了将角色分配给用户,您可以使用以下代码:
(Assuming that the constant "random" is a random index from "Choices". And "Choices" is an array of roles) (假设常数“random”是“Choices”中的随机索引。而“Choices”是角色数组)
var role = message.guild.roles.cache.find(role => role.name === Choices[random]);
message.member.addRole(role);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.