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 way to provide/use own interfaces/classes/... for SlashCommands #66

Open
3 tasks done
Andre601 opened this issue May 7, 2022 · 4 comments
Open
3 tasks done
Labels
module: command Commands module

Comments

@Andre601
Copy link

Andre601 commented May 7, 2022

Issue Checklist

  • I have checked for similar issues on the Issue tracker
  • I have updated to the latest version of JDA-Chewtils.
  • I have checked the branches or the maintainer's PRs for upcoming features and updates.

Affected Modules

Command

Description

It would be useful, if there was a sort of SlashCommandPreProcess method or alike that would allow me (and probably many others) to do the most common checks on a slash command such as if it was from a guild.

What could be somewhat problematic is to decide how stuff is returned... Should it stay a SlashCommandEvent, or should it already be handled (deferReply()?) and then the InteractionHook returned?

I personally would find it cool, if we could "override" the default execute method to have our own instead.

To explain what I mean with that, I want to quickly show an example of what I'm currently using in my bot with message-based commands.
I'm using a different library for those commands called JDA-Command, which provides and uses an interface called AbstractCommand<T>. Since my commands are always with JDA Message objects and only work in Guilds did I "override" it by extending a Command interface I made with it to then override the method with my own:
https://github.com/purrbot-site/PurrBot/blob/2f44f14757bbbd6d82ca29bffa41bb53d42da232/src/main/java/site/purrbot/bot/commands/Command.java

This basically allowed me to reduce commonly made checks and actions and have a bit more safety about things (i.e. that Guild and Member are always present)

Having a similar way in Chewtils would help a lot, because then I could check if the command is from a Guild, obtain user/member and the textchannel instance and if they are null return an error before processing it further...

I hope you get what I mean... I'm not the best at explaining stuff...

@Chew
Copy link
Owner

Chew commented May 7, 2022

Would you like a ISlashCommand interface that you can implement, as long as it has whatever the library requires?

@Andre601
Copy link
Author

Andre601 commented May 7, 2022

Could be a solution.

@Andre601
Copy link
Author

Andre601 commented May 7, 2022

My personal goal is just a way to have Chewtils handle the SlashCommandEvent in one central place and then execute a method for the respective command class that would have stuff...

If I understand interfaces correctly do I currently have a setup like this:

  • Command
    public class CmdSomething extends SlashCommand implements CommandConverter {
    
        private final Bot bot;
    
        public CmdSomething(Bot bot) {
            this.bot = bot;
            
            this.name = "something";
            this.help = "Does something";
        }
        
        @Override
        protected void execute(SlashCommandEvent event) {
            this.convertCommand(event, bot);
        }
        
        @Override
        public void handle(SlashCommandEvent event, InteractionHook hook, Guild guild, TextChannel tc, Member member) {
            // Do stuff here
        }
    }
  • CommandConverter interface
    public interface CommandConverter {
    
        default void convertCommand(SlashCommandEvent event, Bot bot) {
            Guild guild = event.getGuild();
            if (guild == null) {
                CommandUtil.replyError(event, "Command can only be executed within a Guild");
                return;
            }
            
            Member member = event.getMember();
            if (member == null) {
                CommandUtil.replyErrorTranslated(event, bot, guild, "errors.no_member");
                return;
            }
            
            event.deferReply().queue(hook -> handle(event, hook, guild, event.getTextChannel(), member);
        }
        
        void handle(SlashCommandEvent event, InteractionHook hook, Guild guild, TextChannel tc, Member member);
    }

@Chew
Copy link
Owner

Chew commented May 7, 2022

It'd be something like this:

Chewtils has a ISlashCommand

Then you have your own Impl, (SlashCommand will be an impl as well)

Maybe like this:

public class MyCustomSlashCommand implements ISlashCommand {
    public void handle(SlashCommandEvent event) {
        // the cool stuff

        run(); // method in interface
    }
}

Then in your own bot:

public class SuperCommand extends MyCustomSlashCommand {
    // code like normal
}

This is merely a theory, I'm sure something like it is possible.

@Chew Chew added the module: command Commands module label May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: command Commands module
Projects
None yet
Development

No branches or pull requests

2 participants