net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent Java Examples

The following examples show how to use net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuildMemberListener.java    From SkyBot with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onEvent(@Nonnull GenericEvent event) {
    if (event instanceof GuildMemberJoinEvent) {
        this.onGuildMemberJoin((GuildMemberJoinEvent) event);
    } else if (event instanceof GuildMemberRemoveEvent) {
        this.onGuildMemberRemove((GuildMemberRemoveEvent) event);
    } else if (event instanceof GuildMemberRoleRemoveEvent) {
        this.onGuildMemberRoleRemove((GuildMemberRoleRemoveEvent) event);
    } else if (event instanceof GuildMemberRoleAddEvent) {
        this.onGuildMemberRoleAdd((GuildMemberRoleAddEvent) event);
    }
}
 
Example #2
Source File: MantaroListener.java    From MantaroBot with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handles automatic deliver of patreon keys. Should only deliver keys when
 * - An user was already in the guild or just joined and got the "Patreon" role assigned by the Patreon bot
 * - The user hasn't re-joined to get the role re-assigned
 * - The user hasn't received any keys
 * - The user pledged, obviously
 *
 * @param event The event that says that a role got added, obv.
 */
private void handleNewPatron(GuildMemberRoleAddEvent event) {
    //Only in mantaro's guild...
    if (event.getGuild().getIdLong() == 213468583252983809L && !MantaroData.config().get().isPremiumBot()) {
        threadPool.execute(() -> {
            User user = event.getUser();
            //who...
            DBUser dbUser = db.getUser(user);
            PremiumKey currentKey = MantaroData.db().getPremiumKey(dbUser.getData().getPremiumKey());

            if (event.getMember().getRoles().stream().anyMatch(r -> r.getId().equals("290257037072531466"))) {
                if (!dbUser.getData().hasReceivedFirstKey() && (currentKey == null || currentKey.validFor() < 20)) {
                    //Attempt to open a PM and send a key!
                    user.openPrivateChannel().queue(channel -> {
                        //Sellout message :^)
                        channel.sendMessage(EmoteReference.EYES + "Thanks you for donating, we'll deliver your premium key shortly! :heart:").queue(message -> {
                            message.editMessage(EmoteReference.POPPER + "You received a premium key due to your donation to mantaro. " +
                                    "If any doubts, please contact Kodehawa#3457.\n" +
                                    "Instructions: **Apply this key to yourself!**. " +
                                    "This key is a subscription to Mantaro Premium. This will last as long as you pledge. If you want more keys (>$2 donation) " +
                                    "or want to enable the patreon bot (>$4 donation) you need to contact Kodehawa to deliver your keys.\n" +
                                    "To apply this key, run the following command in any channel `~>activatekey " +
                                    //will eventually get linked (see DBUser.java, lines 103-107), patreon webhook can be slower and clash with what I need here.
                                    PremiumKey.generatePremiumKey(user.getId(), PremiumKey.Type.USER, false).getId() + "`\n" +
                                    "Thanks you soo much for donating and helping to keep Mantaro alive! :heart:").queue(sent -> {
                                        dbUser.getData().setHasReceivedFirstKey(true);
                                        dbUser.saveAsync();
                                    }
                            );

                            Metrics.PATRON_COUNTER.inc();
                            //Celebrate internally! \ o /
                            LogUtils.log("Delivered premium key to " + user.getName() + "#" + user.getDiscriminator() + "(" + user.getId() + ")");
                        });
                    }, failure -> LogUtils.log(String.format("User: %s (%s#%s) couldn't receive the key, apply manually when asked!", user.getId(), user.getName(), user.getDiscriminator())));
                }
            }
        });
    }
}
 
Example #3
Source File: EntityBuilder.java    From JDA with Apache License 2.0 5 votes vote down vote up
private void updateMemberRoles(MemberImpl member, List<Role> newRoles, long responseNumber)
{
    Set<Role> currentRoles = member.getRoleSet();
    //Find the roles removed.
    List<Role> removedRoles = new LinkedList<>();
    each:
    for (Role role : currentRoles)
    {
        for (Iterator<Role> it = newRoles.iterator(); it.hasNext(); )
        {
            Role r = it.next();
            if (role.equals(r))
            {
                it.remove();
                continue each;
            }
        }
        removedRoles.add(role);
    }

    if (removedRoles.size() > 0)
        currentRoles.removeAll(removedRoles);
    if (newRoles.size() > 0)
        currentRoles.addAll(newRoles);

    if (removedRoles.size() > 0)
    {
        getJDA().handleEvent(
            new GuildMemberRoleRemoveEvent(
                getJDA(), responseNumber,
                member, removedRoles));
    }
    if (newRoles.size() > 0)
    {
        getJDA().handleEvent(
            new GuildMemberRoleAddEvent(
                getJDA(), responseNumber,
                member, newRoles));
    }
}
 
Example #4
Source File: GuildMemberListener.java    From SkyBot with GNU Affero General Public License v3.0 4 votes vote down vote up
private void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) {
    if (event.getGuild().getIdLong() != Settings.SUPPORT_GUILD_ID) {
        return;
    }

    final long userId = event.getUser().getIdLong();
    final AtomicReference<Patron.Type> typeToSet = new AtomicReference<>(null);

    event.getRoles()
        .stream()
        .map(Role::getIdLong)
        .forEach((roleId) -> {
            // All guild patron
            if (roleId == Settings.GUILD_PATRONS_ROLE) {
                CommandUtils.guildPatrons.add(userId);
                typeToSet.set(Patron.Type.ALL_GUILD);
                return;
            }

            // One guild patron
            if (roleId == Settings.ONE_GUILD_PATRONS_ROLE) {
                CommandUtils.patrons.remove(userId);
                handleNewOneGuildPatron(userId);
                // We assume that the patron already did the steps to register
                return;
            }

            // Tag patron
            if (roleId == Settings.TAG_PATRONS_ROLE) {
                CommandUtils.patrons.remove(userId);
                CommandUtils.tagPatrons.add(userId);
                typeToSet.set(Patron.Type.TAG);
                return;
            }

            // Normal patron
            if (roleId == Settings.PATRONS_ROLE) {
                CommandUtils.patrons.add(userId);
                typeToSet.set(Patron.Type.NORMAL);
            }
        });

    // if we have a type set it in the database
    // Type is set in the database here to prevent un-needed updates
    if (typeToSet.get() != null) {
        variables.getDatabaseAdapter().createOrUpdatePatron(typeToSet.get(), userId, null);
    }
}
 
Example #5
Source File: GroupSynchronizationManager.java    From DiscordSRV with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onGuildMemberRoleAdd(@Nonnull GuildMemberRoleAddEvent event) {
    onGuildMemberRolesChanged("add", event.getMember(), event.getRoles());
}
 
Example #6
Source File: PunishmentListener.java    From Arraybot with Apache License 2.0 2 votes vote down vote up
/**
 * When a member has a role added to them.
 * Used to check if somebody is being muted.
 * @param event The event.
 */
@Override
public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent event) {
    handlePunishment(event, event.getUser(), PunishmentType.MUTE, event.getRoles().toArray(new Role[event.getRoles().size()]), false);
}