正如我之前所展示的,该代码应该给与指定表情符号做出反应的任何人发挥作用,我现在的麻烦是使Reaction Collector
无法理解谁做出了反应并赋予他角色。我当时看着guide,尝试了他们提供的解决方案,但是第一个没有满足我的需要,第二个没有带来用户,而且我也不知道如何解决。 / p>
module.exports = {
name: 'cargo',
description: 'Give a role to an user by reaction',
execute(message, args) {
function getRoleFromMention(mention) {
if (!mention)
console.log('No role was passed');
if (mention.startsWith('<@&') && mention.endsWith('>')) {
mention = mention.slice(3, -1);
return message.guild.roles.cache.get(mention);
}
}
const messageId = args.slice(0, 1);
const roleEmoji = args.slice(1, 2);
const roleMention = args.slice(2, 3);
const roleToGive = getRoleFromMention(`${roleMention}`);
//Bot will react the specified message with the given emoji
console.log('\x1b[35m%s\x1b[0m', 'messageId:', messageId, 'roleEmoji:', roleEmoji, 'roleMention', roleMention); // show each argument on terminal
message.channel.messages.fetch(`${messageId}`).then(msg => msg.react(`${roleEmoji}`));
//Reaction Collector filter
const filter = (reaction, user) => { return reaction.emoji.name === `${roleEmoji}` /*&& user.id === message.author.id*/; };
//Reaction Collector
message.channel.messages.fetch(`${messageId}`)
.then(msg => msg.awaitReactions(filter, { max: 4, time: 20000, errors: ['time'] })
.then(collected => console.log(collected.size))
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
}));
}
}
答案 0 :(得分:0)
查看其他主题,您将拥有所需的一切。 https://discordjs.guide/popular-topics/common-questions.html#how-do-i-add-a-role-to-a-guild-member
答案 1 :(得分:0)
您可以使用Message.createReactionCollector()
来收集消息,然后通过将做出反应的用户的ID与成员ID进行比较来获得做出反应的成员。
//Reaction Collector
message.channel.messages.fetch(`${messageId}`)
.then(msg => {
const collector = msg.createReactionCollector((reaction, user) => !user.bot);
const reactions = {
'?': 'arole',
'?': 'anotherrole'
};
collector.on('collect', (reaction, user) => {
if (!reaction.emoji.name in reactions) return;
const role = reaction.message.guild.roles.cache.find(role => role.name === reactions[reaction.emoji.name]);
const member = reaction.message.guild.members.cache.find(member => member.id === user.id);
member.roles.add(role);
});
});