Skip to content

Commit

Permalink
Add slash buttons (incomplete)
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed Mar 19, 2022
1 parent 7447c35 commit 74582c9
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 96 deletions.
2 changes: 1 addition & 1 deletion CCH.iml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.dv8tion:JDA:5.0.0-alpha.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.dv8tion:JDA:5.0.0-alpha.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.jetbrains:annotations:16.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
Expand Down
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@

<groupId>tech.xigam</groupId>
<artifactId>CCH</artifactId>
<version>1.4.3</version>
<version>1.5.0</version>

<name>Complex Command Handler</name>
<description>A really useful and simple command handler for JDA.</description>
<url>https://github.com/KingRainbow44/Complex-Command-Handler/</url>

<!-- CHANGE LOG -->
<!-- 1.0.2 - Added implementation of deleting commands -->
<!-- 1.0.3 - Fix an issue with option string arguments -->
<!-- 1.0.4 - Added DM ephemeral -->
<!-- 1.1.0 - Added extra options, fixed bugs -->
<!-- 1.1.1 - Reply chain commands -->
<!-- 1.0.2 - Added implementation of deleting commands. -->
<!-- 1.0.3 - Fix an issue with option string arguments. -->
<!-- 1.0.4 - Added DM ephemeral. -->
<!-- 1.1.0 - Added extra options, fixed bugs. -->
<!-- 1.1.1 - Reply chain commands. -->
<!-- 1.2.0 - Update for JDA v5-alpha.5 -->
<!-- 1.3.0 - Trailing prefix-based arguments. -->
<!-- 1.4.0 - Auto-completable arguments. -->
<!-- 1.4.1 - Added command-in-guild check. -->
<!-- 1.4.2 - Add support for other option types. -->
<!-- 1.4.3 - Add support for all channel types. -->
<!-- 1.5.0 - Implement buttons and handle attachments. -->

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down Expand Up @@ -127,7 +128,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-alpha.5</version>
<version>5.0.0-alpha.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/tech/xigam/cch/ComplexCommandHandler.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package tech.xigam.cch;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
Expand Down Expand Up @@ -43,9 +44,6 @@ public final class ComplexCommandHandler extends ListenerAdapter

public Consumer<Interaction> onArgumentError = interaction -> {};

/**
* This constructor should be called in {@link JDABuilder#addEventListeners}
*/
public ComplexCommandHandler(boolean usePrefix) {
this.usePrefix = usePrefix;
}
Expand All @@ -59,6 +57,7 @@ public ComplexCommandHandler registerCommand(BaseCommand command) {
}

public void setJda(JDA jda) {
jda.addEventListener(this);
this.jdaInstance = jda;
}

Expand Down Expand Up @@ -103,12 +102,6 @@ private void executeCommand(String command, Message message2, Member member, Tex
);
}

/*
* Completing commands.
*/



/*
* Handling interactive arguments.
*/
Expand Down Expand Up @@ -188,6 +181,22 @@ public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInterac
command.prepareForCompletion(event, this);
}

@Override
public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
String rawReference = event.getComponentId();
if (!rawReference.startsWith("<"))
return;

var label = rawReference.split("<")[1].split(">")[0];
var command = commands.get(label);
command.prepareForCallback(label, event, this);
}

@Override
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) {

}

/**
* Delete and create commands.
*/
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/tech/xigam/cch/command/BaseCommand.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,83 @@
package tech.xigam.cch.command;

import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
import tech.xigam.cch.ComplexCommandHandler;
import tech.xigam.cch.utils.Interaction;

import java.util.List;

import static tech.xigam.cch.utils.Validation.isUrl;

/**
* An interface used by {@link Command}, {@link SubCommand}, and {@link Alias}
*/
public interface BaseCommand {
/*
* General command information.
*/

String getLabel();

String getDescription();

/*
* Back-end code.
*/

void execute(Interaction interaction);

void prepareForExecution(List<String> arguments, Message message, Member sender, TextChannel channel, boolean skipArguments, ComplexCommandHandler handler);

void prepareForExecution(SlashCommandInteractionEvent event, ComplexCommandHandler handler);

void prepareForCompletion(CommandAutoCompleteInteractionEvent event, ComplexCommandHandler handler);

// void prepareForCallback(MessageReactionAddEvent event, ComplexCommandHandler handler);

void prepareForCallback(String cmdLabel, ButtonInteractionEvent event, ComplexCommandHandler handler);

/**
* Creates a button with proper handling for this command.
*
* @param style The button style.
* @param reference The reference to the button (or a URL).
* @param text The text to display on the button.
* @return The button.
*/
default Button createButton(ButtonStyle style, String reference, String text) {
return Button.of(style, isUrl(reference) ? reference : "<" + this.getLabel().toLowerCase() + ">" + reference, text);
}

/**
* Creates a button with proper handling for this command.
*
* @param style The button style.
* @param reference The reference to the button (or a URL).
* @param emoji The emoji to show on the button.
* @return The button.
*/
default Button createButton(ButtonStyle style, String reference, Emoji emoji) {
return Button.of(style, isUrl(reference) ? reference : "<" + this.getLabel().toLowerCase() + ">" + reference, emoji);
}

/**
* Creates a button with proper handling for this command.
*
* @param style The button style.
* @param reference The reference to the button (or a URL).
* @param text The text to show to the right of the emoji.
* @param emoji The emoji to show on the button.
* @return The button.
*/
default Button createButton(ButtonStyle style, String reference, String text, Emoji emoji) {
return Button.of(style, isUrl(reference) ? reference : "<" + this.getLabel().toLowerCase() + ">" + reference, text, emoji);
}
}
12 changes: 12 additions & 0 deletions src/main/java/tech/xigam/cch/command/Callable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tech.xigam.cch.command;

import tech.xigam.cch.utils.Callback;

/**
* Declares a class as callable, allowing:
* - buttons to work
* - forms to work
*/
public interface Callable {
void callback(Callback callback);
}
19 changes: 15 additions & 4 deletions src/main/java/tech/xigam/cch/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import tech.xigam.cch.ComplexCommandHandler;
import tech.xigam.cch.utils.Argument;
import tech.xigam.cch.utils.Completion;
import tech.xigam.cch.utils.Interaction;
import tech.xigam.cch.utils.InteractiveArguments;
import tech.xigam.cch.utils.*;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -130,6 +128,19 @@ public void prepareForCompletion(CommandAutoCompleteInteractionEvent event, Comp
((Completable) this).complete(new Completion(event));
}

@Override
public void prepareForCallback(String cmdLabel, ButtonInteractionEvent event, ComplexCommandHandler handler) {
if (subCommands.containsKey(cmdLabel)) {
var subCmd = this.getSubCommand(cmdLabel);
if (subCmd instanceof Callable)
((Callable) subCmd).callback(new Callback(event));
return;
}

if (this instanceof Callable)
((Callable) this).callback(new Callback(event));
}

public final Map<String, SubCommand> getSubCommands() {
return subCommands;
}
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/tech/xigam/cch/defaults/DeployCommand.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tech.xigam.cch.defaults;

import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import tech.xigam.cch.command.Arguments;
import tech.xigam.cch.command.Command;
import tech.xigam.cch.utils.Argument;
import tech.xigam.cch.utils.Interaction;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;

Expand All @@ -16,6 +18,11 @@ public DeployCommand() {

protected abstract boolean permissionCheck(Interaction interaction);

@Nullable
protected MessageEmbed embedify(String text) {
return null;
}

@Override
public void execute(Interaction interaction) {
if (!permissionCheck(interaction)) {
Expand All @@ -28,15 +35,22 @@ public void execute(Interaction interaction) {

if (!global && !interaction.isFromGuild()) {
global = true;
interaction.sendMessage("You can't deploy slash commands to a DM, deploying globally instead.");
var embed = this.embedify("You can't deploy slash commands to a DM, deploying globally instead.");
if (embed == null)
interaction.reply("You can't deploy slash commands to a DM, deploying globally instead.");
else interaction.reply(embed);
}

if (delete) {
interaction.getCommandHandler().downsert(global ? null : interaction.getGuild());
interaction.reply("Deleted all commands.");
var embed = this.embedify("Deleted all commands.");
if (embed == null) interaction.reply("Deleted all commands.");
else interaction.reply(embed);
} else {
interaction.getCommandHandler().deployAll(global ? null : interaction.getGuild());
interaction.reply("Deployed all commands.");
var embed = this.embedify("Deployed all commands.");
if (embed == null) interaction.reply("Deployed all commands.");
else interaction.reply(embed);
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/main/java/tech/xigam/cch/utils/Callback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tech.xigam.cch.utils;

import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;

/**
* A callback for buttons and forms.
*/
public final class Callback {
private final boolean isSlash, inGuild;
private final String reference;

private GenericComponentInteractionCreateEvent interactionExecutor = null;

private boolean deferred = false;

public Callback(ButtonInteractionEvent event) {
this.isSlash = true;
this.inGuild = event.isFromGuild();
this.interactionExecutor = event;

var rawReference = event.getComponentId();
this.reference = rawReference.split(">")[1];
}

public String getReference() {
return this.reference;
}

// ---------- UTILITY METHODS ---------- \\

public Callback deferEdit() {
if (this.isSlash)
this.interactionExecutor.deferEdit().queue();
this.deferred = true;
return this;
}

// ---------- REPLY METHODS ---------- \\

public void reply(String message) {

}
}
5 changes: 4 additions & 1 deletion src/main/java/tech/xigam/cch/utils/Completion.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public Completion addChoice(String mapping, Object value) {
}

public void reply() {
this.completeEvent.replyChoices(this.choices).queue();
try {
this.completeEvent.replyChoices(this.choices).queue();
} catch (IllegalStateException ignored) {
}
}
}
Loading

0 comments on commit 74582c9

Please sign in to comment.