Stack Overflow社区的您好会员,
我一直在研究一种功能,该功能在主持人批准使用Auttaja的网守批准用户时会激活。基本上,新用户可以在两个服务器表情符号反应之间进行选择,这将赋予他/她这些角色。但是我在角色和表情符号常量方面遇到了困难。
这是我的代码:
const ApprovalEmbed = new Discord.MessageEmbed()
.setColor('RED')
.setTitle('Hi there new user!')
.setDescription('Please react to this message with either one of the emojis in order to get a role.')
.addFields(
{ name: '\u200B', value: '\u200B' },
{ name: 'The Boomer emoji gives you the "Les boomers normaux" role, and the rat emoji gives you the "The normal Rat Haven dwellers" role.'},
{ name: '\u200B', value: '\u200B' },
{ name: 'Selecting the Boomer emoji will give you access to only the Boomer Haven compartment, and the rat emoji will give you access to the Rat Haven compartment. '},
{ name: '\u200B', value: '\u200B' },
{name: 'To get access to both of these compartments, please consider using the command "^AccessToBoth" to receive the "Access to both compartments" role.'},
)
.setTimestamp()
.setFooter('Time to pick a role!');
const roleOne = guild.roles.cache.find((role) => role.name === "The normal Rat Haven dwellers")
const roleTwo = guild.roles.cache.find((role) => role.name === "Les boomers normaux")
const BoomerEmoji = message.guild.emojis.cache.find(emoji => emoji.name === 'boomer');
const RatEmoji = message.guild.emojis.cache.find(emoji => emoji.name === 'robincutmoment');
client.on('message', async message => {
let AdminRole = message.member.roles.cache.find(role => role.name === "Server Moderators")
let RulerRole = message.member.roles.cache.find(role => role.name === "The Supreme Boomers")
let RatRole = message.member.roles.cache.find(role => role.name === "Rat Majesty Robin")
if (message.member.roles.cache.has(AdminRole)) {
} else if (message.member.roles.cache.has(RulerRole)) {
} else if (message.member.roles.cache.has(RatRole)) {}
if (message.content === `-approve`) {
message.channel.send(BoomerEmoji)
const reactionMessage = await message.channel.send(ApprovalEmbed);
message.channel.send(ApprovalEmbed).then(reactionMessage.react(BoomerEmoji), reactionMessage.react(RatEmoji))
}
const filter = (reaction, user) => {
return [(BoomerEmoji), (RatEmoji)].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === (BoomerEmoji)) {
message.member.roles.add(roleOne)
} else if (reaction.emoji.name === (RatEmoji)) {
message.member.roles.add(roleTwo)
}
})
});
答案 0 :(得分:1)
您的第一个问题出现在此代码段中:
if (message.member.roles.cache.has(AdminRole)) {
// ...
} else if (message.member.roles.cache.has(RulerRole)) {
// ...
} else if (message.member.roles.cache.has(RatRole)) {
// ...
}
Collection.prototype.has()
检查集合中是否存在元素。它具有一个参数:您要检查其存在的元素的 key 。由于roles是由snowflakes映射的,因此您应该传递role ID,而不是entire object。
// `Collection.prototype.has()` is identical to `Map.prototype.has()`
// here's how it works:
const map = new Map();
// 'foo' is the key,
// 'bar' is the value
map.set('foo', 'bar');
console.log(map.has('foo')); // true
console.log(map.has('bar')); // 'bar' is not the key; false
if (message.member.roles.cache.has(AdminRole.id)) {
// ...
} else if (message.member.roles.cache.has(RulerRole.id)) {
// ...
} else if (message.member.roles.cache.has(RatRole.id)) {
// ...
}
如果您愿意,可以使用Collection.prototype.findKey()
而不是普通的Collection.prototype.find()
。此方法几乎相同,除了它返回找到的元素的键而不是值。同样,这几乎没有什么区别。只是另一个选择。
let AdminRole = message.member.roles.cache.findKey(
(role) => role.name === 'Server Moderators'
);
let RulerRole = message.member.roles.cache.findKey(
(role) => role.name === 'The Supreme Boomers'
);
let RatRole = message.member.roles.cache.findKey(
(role) => role.name === 'Rat Majesty Robin'
);
if (message.member.roles.cache.has(AdminRole)) {
// ...
} else if (message.member.roles.cache.has(RulerRole)) {
// ...
} else if (message.member.roles.cache.has(RatRole)) {
// ...
}
您的第二个问题有点类似。 BoomerEmoji
和RatEmoji
是完整的GuildEmoji
对象。但是,在这一行:
return (
[BoomerEmoji, RatEmoji].includes(reaction.emoji.name) &&
user.id === message.author.id
);
这些行:
if (reaction.emoji.name === BoomerEmoji) {
message.member.roles.add(roleOne);
} else if (reaction.emoji.name === RatEmoji) {
message.member.roles.add(roleTwo);
}
您正在将其与另一个表情符号的 name 属性进行比较。此修复非常简单,只需声明每个变量时只需要name
属性即可。
const BoomerEmoji = message.guild.emojis.cache.find(
(emoji) => emoji.name === 'boomer'
).name;
const RatEmoji = message.guild.emojis.cache.find(
(emoji) => emoji.name === 'robincutmoment'
).name;