我一直在尝试使用允许用户对消息做出反应并获得角色的命令在discord.js中编写一个discord机器人。这是我当前的代码。我不确定如何在用户添加反应时将其赋予角色,以及当用户的反应删除了它所带走的角色后的位置。
const { RichEmbed } = require('discord.js');
const Discord = require("discord.js");
const colors = require("../colors");
const botconfig = require("../botconfig");
exports.run = async (client, message, args) => {
await message.delete().catch(O_o=>{});
const role1 = message.guild.roles.get('688477558982836344'); // Replace with Updates ID
const role2 = message.guild.roles.get('688477690344374283'); // Replace with Events ID
const role3 = message.guild.roles.get('688477467840872452'); // Replace with QOTD ID
const role4 = message.guild.roles.get('688477868078137369'); // Replace with Polls ID
const role5 = message.guild.roles.get('687498488295981069'); // Replace with Social Media ID
const embed = new Discord.RichEmbed()
.setTitle('Pinged Roles')
.setDescription(`
Tip: *Double React to remove a role*
❤️ ${role1.toString()}
? ${role2.toString()}
? ${role3.toString()}
? ${role4.toString()}
? ${role5.toString()}
`)
.setColor(colors.purple)
.setFooter('mc.advancius.net')
message.channel.send(embed).then(async msg => {
await msg.react('❤️');
await msg.react('?');
await msg.react('?');
await msg.react('?');
await msg.react('?');
});
}
module.exports.help = {
name: "roles"
};
答案 0 :(得分:1)
从您的代码看来,您似乎正在使用Discord.js v11,但我还将包括v12的代码,因为这是最新版本。
您需要使用Client
的{{3}}和messageReactionAdd
事件,因为messageReactionRemove
没有remove
事件。
// I added this object because I prefer having this than having lots of repeated code
// you can still do this the way you were doing before
const roles = {
'❤️': '688477558982836344',
'?': '688477690344374283',
'?': '688477467840872452',
'?': '688477868078137369',
'?': '687498488295981069'
}
exports.run = async (client, message, args) => {
await message.delete()
const embed = new RichEmbed()
.setTitle('Pinged Roles')
.setDescription(`
Tip: *Double React to remove a role*
${Object.entries(roles)
.map(([emoji, id]) => `${emoji} ${message.guild.roles.get(id).toString()}`)
.join('\n')}
`)
.setColor(colors.purple)
.setFooter('mc.advancius.net')
message.channel.send(embed).then(async msg => {
// react with every emoji
for (const emoji of Object.keys(roles)) await msg.react(emoji)
client.on('messageReactionAdd', ({emoji, message}, user) => {
// if the message that the user reacted to is the same as the message with
// the reactions, add the role
if (message.id === msg.id) message.guild.members.get(user.id).addRole(roles[emoji.name])
})
client.on('messageReactionRemove', ({emoji, message}, user) => {
// same as above but remove the role
if (message.id === msg.id) message.guild.members.get(user.id).removeRole(roles[emoji.name])
})
})
}
您可以使用ReactionCollector
,因为createReactionCollector
有一个ReactionCollector
事件,该事件在删除反应后触发。
// I added this object because I prefer having this than having lots of repeated code
// you can still do this the way you were doing before
const roles = {
'❤️': '688477558982836344',
'?': '688477690344374283',
'?': '688477467840872452',
'?': '688477868078137369',
'?': '687498488295981069'
}
exports.run = async (client, message, args) => {
await message.delete()
// because RoleManager.fetch is async
const rolesArray = await Promise.all(Object.entries(roles)
.map(async ([emoji, id]) => `${emoji} ${(await message.guild.roles.fetch(id)).toString()}`)
)
const embed = new MessageEmbed()
.setTitle('Pinged Roles')
.setDescription(`
Tip: *Double React to remove a role*
${rolesArray.join('\n')}
`)
.setColor(colors.purple)
.setFooter('mc.advancius.net')
message.channel.send(embed).then(async msg => {
const emojis = Object.keys(roles)
for (const emoji of emojis) await msg.react(emoji)
// only collect reactions that are the role emojis
const collector = msg.createReactionCollector(({emoji}) => emojis.includes(emoji.name))
collector.on('collect', ({emoji, message}, user) => {
message.guild.members.fetch(user).then(member => {
member.roles.add(roles[emoji.name])
})
})
collector.on('remove', ({emoji, message}, user) => {
message.guild.members.fetch(user).then(member => {
member.roles.remove(roles[emoji.name])
})
})
})
}