Skip to content

Commit

Permalink
Finish basic implementation of buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed Mar 19, 2022
1 parent 74582c9 commit 00f0f2b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 28 deletions.
6 changes: 0 additions & 6 deletions src/main/java/tech/xigam/cch/ComplexCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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 @@ -192,11 +191,6 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
command.prepareForCallback(label, event, this);
}

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

}

/**
* Delete and create commands.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/tech/xigam/cch/command/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 tech.xigam.cch.ComplexCommandHandler;
import tech.xigam.cch.utils.Interaction;

Expand Down Expand Up @@ -41,4 +42,9 @@ public void prepareForExecution(List<String> arguments, Message message, Member
public void prepareForCompletion(CommandAutoCompleteInteractionEvent event, ComplexCommandHandler handler) {
aliasOf.prepareForCompletion(event, handler);
}

@Override
public void prepareForCallback(String cmdLabel, ButtonInteractionEvent event, ComplexCommandHandler handler) {
aliasOf.prepareForCallback(cmdLabel, event, handler);
}
}
2 changes: 0 additions & 2 deletions src/main/java/tech/xigam/cch/command/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public interface BaseCommand {

void prepareForCompletion(CommandAutoCompleteInteractionEvent event, ComplexCommandHandler handler);

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

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

/**
Expand Down
83 changes: 74 additions & 9 deletions src/main/java/tech/xigam/cch/utils/Callback.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package tech.xigam.cch.utils;

import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.interactions.components.ActionRow;

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

private GenericComponentInteractionCreateEvent interactionExecutor = null;
private final GenericComponentInteractionCreateEvent interactionExecutor;

private boolean deferred = false;
private Type deferred = Type.NONE;

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

var rawReference = event.getComponentId();
Expand All @@ -30,15 +29,81 @@ public String getReference() {
// ---------- UTILITY METHODS ---------- \\

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

public Callback deferReply() {
this.interactionExecutor.deferReply().queue();
this.deferred = Type.REPLY;
return this;
}

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

public void edit(String message) {
this.send(message, Type.EDIT);
}

public void reply(String message) {

this.send(message, Type.REPLY);
}

public void edit(MessageEmbed embed) {
this.send(embed, Type.EDIT);
}

public void reply(MessageEmbed embed) {
this.send(embed, Type.REPLY);
}

/**
* Internal message sending/updating method.
*
* @param message The message to send.
*/
private void send(Object message, Type type) {
switch (this.deferred) {
case EDIT -> {
var hook = this.interactionExecutor.getHook();
if (message instanceof String) {
hook.editOriginal((String) message).queue();
} else if (message instanceof MessageEmbed) {
hook.editOriginalEmbeds((MessageEmbed) message).queue();
} else if (message instanceof ActionRow) {
hook.editOriginalComponents((ActionRow) message).queue();
}
}

case REPLY -> {
var hook = this.interactionExecutor.getHook();
if (message instanceof String) {
hook.sendMessage((String) message).queue();
} else if (message instanceof MessageEmbed) {
hook.sendMessageEmbeds((MessageEmbed) message).queue();
}
}

case NONE -> {
if (type == Type.EDIT) {
if (message instanceof String) {
this.interactionExecutor.editMessage((String) message).queue();
} else if (message instanceof MessageEmbed) {
this.interactionExecutor.editMessageEmbeds((MessageEmbed) message).queue();
}
} else {
if (message instanceof String) {
this.interactionExecutor.reply((String) message).queue();
} else if (message instanceof MessageEmbed) {
this.interactionExecutor.replyEmbeds((MessageEmbed) message).queue();
}
}
}
}
}

private enum Type {
NONE, EDIT, REPLY
}
}
43 changes: 32 additions & 11 deletions src/main/java/tech/xigam/cch/utils/Interaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageAction;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import tech.xigam.cch.ComplexCommandHandler;
import tech.xigam.cch.command.Arguments;
import tech.xigam.cch.command.BaseCommand;
Expand Down Expand Up @@ -211,11 +214,11 @@ public Interaction sendMessage(MessageEmbed message) {
getChannel().sendMessageEmbeds(message).queue();
return this;
}

public void reply(String message) {
this.reply(message, true);
}

public void reply(MessageEmbed embed) {
this.reply(embed, true);
}
Expand Down Expand Up @@ -249,27 +252,45 @@ public void run() {
private void send(Object message, boolean mentionUser) {
if (this.isSlash()) {
if (this.isDeferred()) {
WebhookMessageAction<Message> send;

if (message instanceof String)
this.slashExecutor.getHook().sendMessage((String) message).addActionRow(this.buttons).queue();
else
this.slashExecutor.getHook().sendMessageEmbeds((MessageEmbed) message).addActionRow(this.buttons).queue();
send = this.slashExecutor.getHook().sendMessage((String) message);
else send = this.slashExecutor.getHook().sendMessageEmbeds((MessageEmbed) message);

if (!this.buttons.isEmpty()) send = send.addActionRow(this.buttons);
send.queue();
} else {
ReplyCallbackAction send;

if (message instanceof String)
this.slashExecutor.reply((String) message).addActionRow(this.buttons).setEphemeral(this.isEphemeral()).queue();
send = this.slashExecutor.reply((String) message);
else
this.slashExecutor.replyEmbeds((MessageEmbed) message).addActionRow(this.buttons).setEphemeral(this.isEphemeral()).queue();
send = this.slashExecutor.replyEmbeds((MessageEmbed) message);

if (!this.buttons.isEmpty()) send = send.addActionRow(this.buttons);
send.setEphemeral(this.isEphemeral()).queue();
}
} else {
if (this.isEphemeral() && this.sendToDMs) {
this.getMember().getUser().openPrivateChannel().queue(privateChannel -> {
MessageAction send;

if (message instanceof String)
privateChannel.sendMessage((String) message).queue();
else privateChannel.sendMessageEmbeds((MessageEmbed) message).queue();
send = privateChannel.sendMessage((String) message);
else send = privateChannel.sendMessageEmbeds((MessageEmbed) message);

if (!this.buttons.isEmpty()) send = send.setActionRow(this.buttons);
send.queue();
});
} else {
MessageAction send;
if (message instanceof String)
this.getMessage().reply((String) message).mentionRepliedUser(mentionUser).queue();
else this.getMessage().replyEmbeds((MessageEmbed) message).mentionRepliedUser(mentionUser).queue();
send = this.getMessage().reply((String) message);
else send = this.getMessage().replyEmbeds((MessageEmbed) message);

if (!this.buttons.isEmpty()) send = send.setActionRow(this.buttons);
send.mentionRepliedUser(mentionUser).queue();
}
}
}
Expand Down

0 comments on commit 00f0f2b

Please sign in to comment.