Search code examples
javadiscorddiscord-jda

Get a boolean of a map


I have a private final Map<Long, Boolean> rpenabled = new HashMap<>(); and did this so it sets it to true when the bot is online or got added to a server:

public void onGuildReady(@NotNull GuildReadyEvent event) {
        rpenabled.get(event.getGuild().getIdLong());
        rpenabled.get(event.getGuild().getIdLong());

        rpenabled.put(event.getGuild().getIdLong(), true);
        gifenabled.put(event.getGuild().getIdLong(), false);
    }

after that i wanted to use the map in an onMessageReceivedEvent and tried that with

if (rpenabled.values().contains(true)) {

                    rpenabled.put(event.getGuild().getIdLong(), false);

                    event.getChannel().sendMessage("<:relationships_true:1075825708355555578> The `Roleplay Commands` module has successfully been **disabled**.").queue();

                }

and

if (rpenabled.containsKey(true)) {

                    rpenabled.put(event.getGuild().getIdLong(), false);

                    event.getChannel().sendMessage("<:relationships_true:1075825708355555578> The `Roleplay Commands` module has successfully been **disabled**.").queue();

                }

after I implemented the maps with

public void onMessageReceived(@NotNull MessageReceivedEvent event) {

        rpenabled.get(event.getGuild().getIdLong());
        gifenabled.get(event.getGuild().getIdLong());

It's not working properly though. Does anyone know why and how I can fix that?


Solution

  • rpenabled.containsKey(true) will always return false because the keys in your map are Long.

    And rpenabled.values().contains(true) will return true if any of your map entries has a true value.

    You probably want to query the map with rpenabled.get(event.getGuild().getIdLong()) which will return the boolean value associated with the id of the events guild.

    Basically a Map<Long, Boolean> is not that different from a Map<Long, Int>: you can use put(key, value) to store a value into the map and you use get(key) to retrieve the value associated with a key. The only distinction is the type of the value.


    Additional note:

    I don't understand what you want to achieve with

        rpenabled.get(event.getGuild().getIdLong());
        gifenabled.get(event.getGuild().getIdLong());
    

    in the onMessageReceived() method or with the repeated

        rpenabled.get(event.getGuild().getIdLong());
        rpenabled.get(event.getGuild().getIdLong());
    

    in the onGuildReady() method. Those statements access the map but throw away the result.