sxcybot

Discord bot for OSRS based channels
git clone git://git.wimdupont.com/sxcybot.git
Log | Files | Refs | README | LICENSE

commit 61664cd8cdcb998cda58ad7b5680c10e95306c7e
parent a34d307382de5a14aa1efaddb2207b2a8bec392b
Author: Wim Dupont <wim@wimdupont.com>
Date:   Tue, 31 Jan 2023 20:30:06 +0100

assign multiple roles at once

Diffstat:
Msrc/main/java/com/sxcy/sxcybot/listeners/admin/RoleAssignListener.java | 55++++++++++++++++++++++++++++++-------------------------
1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/sxcy/sxcybot/listeners/admin/RoleAssignListener.java b/src/main/java/com/sxcy/sxcybot/listeners/admin/RoleAssignListener.java @@ -43,10 +43,9 @@ public class RoleAssignListener implements Listener { PrivateChannel privateChannel = event.getMember().getUser().openPrivateChannel().complete(true); EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setColor(Color.RED); - embedBuilder.setTitle("Role assigner"); discordMemberFinderUtil.onMemberFoundVerification(event, embedBuilder, privateChannel, member -> { embedBuilder.clearFields(); - embedBuilder.setTitle("Type the number of the role to add, if the member already has this role it will get removed instead."); + embedBuilder.setTitle("Type the numbers of the roles to add separated by a whitespace. If the member already has any role, it will get removed instead."); List<GuildRole> rolesToAdd = guildRoleService.findAllByElevationGreaterThanEqual(ADDED_ROLE_ELEVATION) .stream().sorted(Comparator.nullsLast(Comparator.comparing(GuildRole::getOrderValue))).toList(); for (GuildRole guildRole : rolesToAdd) { @@ -55,36 +54,42 @@ public class RoleAssignListener implements Listener { privateChannel.sendMessageEmbeds(embedBuilder.build()).queue(); eventWaiterUtil.waitForPrivateChannelEvent(roleReceiver -> { String roleMessage = roleReceiver.getMessage().getContentRaw(); - try { - int roleNumber = Integer.parseInt(roleMessage); - Optional<GuildRole> discordRole = rolesToAdd.stream().filter(f -> roleNumber == f.getOrderValue()).findFirst(); - if (discordRole.isPresent()) { - Optional<Role> role = event.getGuild().getRoles().stream().filter(f -> discordRole.get().getName().equalsIgnoreCase(f.getName())).findFirst(); - if (role.isPresent()) { - String action; - if (member.getRoles().contains(role.get())) { - event.getGuild().removeRoleFromMember(member, role.get()).queue(); - action = "removed"; +// try { + String[] roleNumbers = roleMessage.split(" "); + embedBuilder.clearFields(); + embedBuilder.setTitle("Role assigner:"); + for (String roleNumber : roleNumbers) { + try { + Optional<GuildRole> discordRole = rolesToAdd.stream().filter(f -> Integer.parseInt(roleNumber) == f.getOrderValue()).findFirst(); + if (discordRole.isPresent()) { + Optional<Role> role = event.getGuild().getRoles().stream().filter(f -> discordRole.get().getName().equalsIgnoreCase(f.getName())).findFirst(); + if (role.isPresent()) { + String action; + if (member.getRoles().contains(role.get())) { + event.getGuild().removeRoleFromMember(member, role.get()).queue(); + action = "removed"; + } else { + event.getGuild().addRoleToMember(member, role.get()).queue(); + action = "assigned"; + } + embedBuilder.addField(String.format("\"%s\" has been succesfully %s.", discordRole.get().getName(), action), "", false); + event.getChannel().sendMessage(String.format("Role \"%s\" has been %s to %s by %s", + role.get().getName(), action, member.getUser(), event.getMember().getUser().getName())).queue(); } else { - event.getGuild().addRoleToMember(member, role.get()).queue(); - action = "assigned"; + privateChannel.sendMessage("No such role is available in the channel, please contact your channel owner.").queue(); } - embedBuilder.clearFields(); - embedBuilder.addField(String.format("Role has been succesfully %s.", action), "", false); - privateChannel.sendMessageEmbeds(embedBuilder.build()).queue(); - event.getChannel().sendMessage(String.format("Role \"%s\" has been %s to %s by %s", - role.get().getName(), action, member.getUser(), event.getMember().getUser().getName())).queue(); } else { - privateChannel.sendMessage("No such role is available in the channel, please contact your channel owner.").queue(); + privateChannel.sendMessage(String.format("%s is not in the suggested list, please try again later.", roleNumber)).queue(); } - } else { - privateChannel.sendMessage(String.format("%s is not in the suggested list, please try again later.", roleMessage)).queue(); + } catch (NumberFormatException e) { + privateChannel.sendMessage(String.format("%s is not a valid number, please try again later.", roleNumber)).queue(); } - } catch (NumberFormatException e) { - privateChannel.sendMessage(String.format("%s is not a valid number, please try again later.", roleMessage)).queue(); } + if (embedBuilder.getFields().isEmpty()) { + embedBuilder.addField("No roles changed.", "", false); + } + privateChannel.sendMessageEmbeds(embedBuilder.build()).queue(); }, event, privateChannel); - }); } catch (RateLimitedException e) { log.error(e.getMessage(), e);