当前,用户可以选择在本节中指定的尽可能多的角色,但是,我希望能够执行以下操作: (roleA OR roleB)AND(roleC OR roleD)AND roleE等
以上所有内容均应通过检查他们已经提交的反应并在新选择与当前选择相抵触时将其删除来触发。
用户可以通过对指定的消息进行响应来将响应角色应用于自己。根据是否做出特定选择,可以添加/删除其他角色,即
你几岁? -18-24 -25-30 -31岁以上
您的性别是? -男 -女
您在哪个大陆? -IDK,我快用完了 -假问题 -填写空间。
如果用户单击25-30,但随后意识到自己是24,则单击该按钮,我希望删除先前的反应和角色,而无需手动干预。 不仅只有一个选项可用,因此还希望有多个选择。
bot.on("raw", event =>{
console.log(event);
const eventName = event.t;
if(eventName === 'MESSAGE_REACTION_ADD')
{
if(event.d.message_id === '<REMOVED ID>')
{
var reactionChannel = bot.channels.get(event.d.channel_id);
if(reactionChannel.messages.has(event.d.message_id))
return;
else {
reactionChannel.fetchMessage(event.d.message_id)
.then(msg => {
var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
var user = bot.users.get(event.d.user_id);
bot.emit('messageReactionAdd', msgReaction, user);
})
.catch(err => console.log(err));
}
}
}
else if (eventName === 'MESSAGE_REACTION_REMOVE')
{
if(event.d.message_id === '<REMOVED ID>')
{
var reactionChannel = bot.channels.get(event.d.channel_id);
if(reactionChannel.messages.has(event.d.message_id))
return;
else{
reactionChannel.fetchMessage(event.d.message_id)
.then(msg => {
var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
var user = bot.users.get(event.d.user_id);
bot.emit('messageReactionRemove', msgReaction, user);
})
.catch(err => console.log(err));
}
}
}
});
bot.on('messageReactionAdd', (messageReaction, user) => {
var roleName = messageReaction.emoji.name;
var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase());
if(role)
{
var member = messageReaction.message.guild.members.find(member => member.id === user.id);
if(member)
{
member.addRole(role.id);
console.log('Success. Added role.');
}
}
});
bot.on('messageReactionRemove', (messageReaction, user) => {
var roleName = messageReaction.emoji.name;
var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase());
if(role)
{
var member = messageReaction.message.guild.members.find(member => member.id === user.id);
if(member)
{
member.removeRole(role.id);
console.log('Success. Removed role.');
}
}
});```
答案 0 :(得分:1)
在您的messageReactionAdd
事件中,您可以尝试查找与特定选择相对应的用户所应用的反应或角色。如果有一个,则意味着他们已经选择了该答案。然后,您可以在将新角色添加到用户之前将其删除。如果没有,则代码应照常继续。
bot.on('messageReactionAdd', async (messageReaction, user) => { // async needed for 'await'
const name = messageReaction.emoji.name.toLowerCase();
const message = messageReaction.message;
const role = message.guild.roles.find(role => role.name.toLowerCase() === name);
const member = message.guild.member(user);
if (!role || !member) return;
const emojis = message.reactions.map(emoji => emoji.name);
const conflictingReaction = message.reactions.find(reaction => reaction.users.get(user.id) && emojis.includes(reaction.emoji.name));
const conflictingRole = member.roles.find(role => emojis.includes(role.name.toLowerCase());
try {
if (conflictingReaction || conflictingRole) {
await conflictingReaction.remove(user);
await member.removeRole(conflictingRole);
}
await member.addRole(role);
console.log('Success.');
} catch(err) {
console.error(err);
}
});