I'm trying to make a mute command for a discord bot where it removes all the user's roles and gives them the Muted Role and then after a certain amount of time it gives them their old roles back. I am trying to use a Hashmap to store the player's roles and then give it back but it seems to not work. If anyone here could help I would be really thankful. Here is my code:
public static HashMap<List, Role> roleMap = new HashMap<>();
@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
String command = event.getName();
if (command.equalsIgnoreCase("mute")) {
Member user = event.getOption("user").getAsMember();
roleMap.put(event.getOption("user").getAsMember().getRoles(), event.getGuild().getRoleById(id));
event.getGuild().modifyMemberRoles(event.getOption("user").getAsMember(), event.getGuild().getRoleById(id)).queue();
event.getChannel().sendMessage(event.getOption("user").getAsUser().getAsMention() + " has been muted").queue();
if (!event.getMember().hasPermission(Permission.MANAGE_ROLES)) {
event.reply("You do not have the required permissions to use this command!").setEphemeral(true).queue();
}
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
event.getGuild().removeRoleFromMember(event.getOption("user").getAsUser(), event.getGuild().getRoleById("id")).queue();
event.getGuild().addRoleToMember(event.getOption("user").getAsUser(), roleMap.get(event.getOption("user").getAsMember().getRoles()));
}
},
event.getOption("duration").getAsLong() * 1000
);
}
}