我正在尝试建立一个赋予反应角色的系统,问题是当我转到另一个消息(属于另一个类别)并且我使用与消息中做出反应的表情符号反应时,角色...
msg.guild.fetchMembers().then(fetchedGuild => {
const totalOnline = fetchedGuild.members.filter(member => member.presence.status === "online");
const channel = client.channels.find('welcome', '?bienvenido')
client.on('messageReactionAdd', (reaction, user) => {
let limit = (`${totalOnline.size}`);
if(message.channel.type == "text" && message.channel.name.toLowerCase() == "?bienvenido")
{
if (reaction.emoji.name == '?')
{
const guildMember = reaction.message.guild.members.get(user.id);
var role = message.guild.roles.find(role => role.name === "?Programador?️");
guildMember.addRole(role);
}
};
});
client.on('messageReactionRemove', (reaction, user) => {
const channel = client.channels.find(c => c.name === '?bienvenido');
const id = channel ? channel.id : null;
if(reaction.emoji.name == '?' && id === "681933993092055053"){
const guildMember = reaction.message.guild.members.get(user.id);
var role = message.guild.roles.find(role => role.name === "?Programador?️");
guildMember.removeRole(role)
}
});
});
答案 0 :(得分:0)
讨论之后,我使用事件raw
创建了一个新代码,这使您的代码更具可读性,并且工作方式更好!
// the event raw will receive EVERY event from <client>
client.on("raw", data => {
// just receive those two events
if (!["MESSAGE_REACTION_REMOVE", "MESSAGE_REACTION_ADD"].includes(data.t)) return;
// fetch the guild channel
const guild = client.guilds.get(data.d.guild_id);
// fetch the channel from the guild
const channel = guild.channels.get(data.d.channel_id);
// fetch who reacted the message
const reactMember = guild.members.get(data.d.user_id);
// if the reactMember is a bot, just return
if (reactMember.user.bot) return;
// just look at the channel with the ID ...
if (channel.id !== "668236577369096202") return;
// and just look at the message with the ID ...
if (data.d.message_id !== "682481892502929430") return;
// Roles to add by the emoji name
const RoleToAdd = {
"?": "682480577034846208",
"?": "682480474295500803"
}
// fetch the role id by the emoji name
const roleID = RoleToAdd[data.d.emoji.name];
// if you want to put more roles to be added you can just use the emoji as the KEY and the ROLE ID as the VALUE
// "emoji": "role id"
// and if you want to work with custom emojis:
/*
const RoleToAdd = {
"emoji ID": "role ID"
}
const roleID = RoleToAdd[data.d.emoji.id];
*/
// invoke the fuction by the event name
if (data.t === "MESSAGE_REACTION_ADD") ReactionAdded();
if (data.t === "MESSAGE_REACTION_REMOVE") ReactionRemoved();
// if the member add a reaction into the message
function ReactionAdded() {
// check if the roleID exists by the emoji and if the member DOESNT have that role
if (roleID && !reactMember.roles.has(roleID)) {
// add him into the role
reactMember.addRole(roleID)
};
}
// if the member removed a reaction from the message
function ReactionRemoved() {
// check if the roleID exysts by the emoji and if the member HAS that role
if (roleID && reactMember.roles.has(roleID)) {
// remove him from the role
reactMember.removeRole(roleID);
}
}
});
// Hope this helps!