Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add roles based on existing reaction #25

Open
p0358 opened this issue Jul 12, 2021 · 2 comments
Open

[Feature Request] Add roles based on existing reaction #25

p0358 opened this issue Jul 12, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@p0358
Copy link

p0358 commented Jul 12, 2021

It would be very cool if we could execute a function that would go through the existing reactions on the configured messages, and assign any roles that are missing based on those. Use case is that our bot may have been offline for some time, or we're migrating from another bot that missed many reactions. This could ensure there's nothing missing, I think it would make sense to have such an option

@danirod danirod added the enhancement New feature or request label Jul 12, 2021
@danirod
Copy link
Member

danirod commented Jul 13, 2021

Yeah, I've been thinking about something like this too. Our bot doesn't have downtime issues, but there are cases such as reactions from people who isn't anymore in the server that could be removed with a catchUp() function to sync the roles to the reactions.

@p0358
Copy link
Author

p0358 commented Jul 13, 2021

Would be good if removing the reactions of missing people was optional. And while bot may not have downtime issues, the Discord API having ones isn't that rare, and then these reactions may quickly get missed.
Here is an example code snippet on how I did this if that helps (I just put it quickly together to get the job done, so it's ugly and not safe against edge cases, but TS will catch all of these):

msg.guild.channels.resolve("CHAN_ID").messages.fetch("MSG_ID").then(message =>
            {
                    message.reactions.cache.find(r=>r.emoji.name==="🖥️").users.fetch({after:"LAST_USER_ID"}).then(r=>{
                    r.each(u=>{
                        let gm=msg.guild.members.resolve(u.id);
                        if(gm&&!gm.roles.cache.has("ROLE_ID")){
                            gm.roles.add("ROLE_ID");
                            msg.reply('adding missing role to:' + gm.user.tag);
                        };
                    });
                    msg.reply('last: ' + r.last().id + ', size: ' + r.size);
                });
            }
        );

Generally users.fetch() should initially have no params, but it returns up to 100 users at once, so then you gotta use the r.last().id to supply to the after param to get the next ones. If returned amount is <100 or is 0, then it was the last portion. Checking against roles.cache is important, because otherwise rate limits are hit very frequently, and even if something isn't in the cache, it doesn't hurt to check it in this case (different story with removal...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants