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

Add a method that check disabled and enabled methods before running the button's... #5

Open
github-actions bot opened this issue Apr 24, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

// TODO: Add a method that check disabled and enabled methods before running the button's code

/*
 * Copyright (c) 2021 - 2022. Greazi - All rights reservered
 *
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 */

package com.greazi.discordbotfoundation.handlers.buttons;

import com.greazi.discordbotfoundation.debug.Debugger;
import com.greazi.discordbotfoundation.settings.SimpleSettings;
import com.greazi.discordbotfoundation.utils.SimpleEmbedBuilder;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.hooks.SubscribeEvent;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Objects;


// TODO: Make a proper debug message return
public class ButtonHandler extends ListenerAdapter {

    private final HashMap<String, SimpleButton> buttonList = new HashMap<>();

    public ButtonHandler addButtonListener(SimpleButton module) {

        buttonList.put(module.getbutton(), module);

        return this;
    }


    // TODO: Add a method that check disabled and enabled methods before running the button's code
    @Override
    @SubscribeEvent
    public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
        Debugger.debug("Buttons", "A button has been pressed");
        // Retrieve the command class from the command that has been run
        SimpleButton module = buttonList.get(event.getId());

        if (module == null) {
            event.replyEmbeds(new SimpleEmbedBuilder("ERROR - button not found")
                    .text("The button you used does not exist or hasn't been activated!",
                            "Please contact an admin and report this error!")
                    .error()
                    .setFooter("")
                    .build()).setEphemeral(true).queue();
            return;
        }

        Debugger.debug("Buttons", "Found event; " + module);

        if (module.getGuildOnly() && !Objects.requireNonNull(event.getGuild()).getId().equals(SimpleSettings.Bot.MainGuild())){
            return;
        }

        if (event.getTextChannel().isNSFW() && !module.getNsfwOnly()){
            return;
        }

        Debugger.debug("Button", "  Executing command logic");
        module.execute(event);
    }
}
@github-actions github-actions bot added the todo label Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants