Попытка сделать: реагировать, чтобы дать роль с раздором js - PullRequest
1 голос
/ 27 февраля 2020

Я пытаюсь создать систему предоставления роли с реакцией, проблема в том, что, когда я go обращаюсь к другому сообщению (в другой категории) и я реагирую тем же смайликом, который я вставил в сообщение, чтобы реагировать, это дает роль ...

  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)
                      }    
                });               
            });

1 Ответ

0 голосов
/ 27 февраля 2020

После того, как мы поговорим, я создал новый код, используя событие 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!
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...