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

Interactions stuff #8

Merged
merged 9 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,43 @@
import org.jetbrains.annotations.Nullable;

@Name("Defer Interaction")
@Description({"Only usable in interaction event, currently button click & dropdown update event!",
"\nThis will force the interaction to be acknowledge, sending a success message to Discord.",
@Description({"Only usable in interaction event, currently button click/dropdown update/modal event!",
"\nThis will force the interaction to be acknowledge, you have 3 seconds to do so, the effect will send a success message to Discord or hold the interaction to send a message later.",
"\nKeep in mind that replying in an interaction event will automatically defer the interaction, and therefore you don't need to defer it.",
"\nIf you need to wait more than 3 seconds use the and wait pattern",
"\nAn interaction can only be deferred once!"})
@Examples("defer the interaction")
@Examples({"defer the interaction",
"\n defer the interaction and wait",
"\n defer the interaction and wait silently"})
public class EffDeferInteraction extends Effect {

static {
Skript.registerEffect(
EffDeferInteraction.class,
"(acknowledge|defer) [the] interaction"
"(acknowledge|defer) [the] interaction [and wait (1¦silently)]"
);
}

private NodeInformation node;

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {

if (!EasyElement.containsInterfaces(InteractionEvent.class)) {
Skript.error("The defer interaction effect can only be used in interaction events!");
return false;
}

node = new NodeInformation();

isEphemeral = parseResult.mark == 1;
shouldwait = parseResult.expr.contains("and wait");

return true;
}

private boolean isEphemeral;
private boolean shouldwait;

@Override
protected void execute(@NotNull Event e) {
Expand All @@ -42,11 +65,15 @@ protected void execute(@NotNull Event e) {
if (event instanceof GenericComponentInteractionCreateEvent) {

GenericComponentInteractionCreateEvent clickEvent = (GenericComponentInteractionCreateEvent) event;
clickEvent.deferEdit().queue(null, ex -> DiSky.getErrorHandler().exception(e, ex));

} else if (event instanceof ModalInteractionEvent) {

ModalInteractionEvent clickEvent = (ModalInteractionEvent) event;

if (shouldwait) {
clickEvent.deferReply(isEphemeral).queue(null, ex -> DiSky.getErrorHandler().exception(e, ex));
return;
}
clickEvent.deferEdit().queue(null, ex -> DiSky.getErrorHandler().exception(e, ex));

}
Expand All @@ -58,18 +85,4 @@ protected void execute(@NotNull Event e) {
return "defer the interaction";
}

private NodeInformation node;

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {

if (!EasyElement.containsInterfaces(InteractionEvent.class)) {
Skript.error("The defer interaction effect can only be used in interaction events!");
return false;
}

node = new NodeInformation();

return true;
}
}
22 changes: 16 additions & 6 deletions src/main/java/info/itsthesky/disky/elements/effects/ReplyWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
Expand Down Expand Up @@ -81,19 +82,28 @@ public void runEffect(Event e, Bot bot) {
if (isInInteraction) {

final IReplyCallback event = (IReplyCallback) ((InteractionEvent) e).getInteractionEvent();
final InteractionHook hook = event.getHook();
final Object rawMessage = parseSingle(exprMessage, e, null);
final MessageBuilder message = JDAUtils.constructMessage(rawMessage);
if (anyNull(event, rawMessage, message)) {
restart();
return;
}

event.reply(message.build())
.addActionRows(formatted)
.setEphemeral(hidden)
.queue(v -> restart(), ex -> {
if (!hook.isExpired()) {
hook.editOriginal(message.build())
.queue(v -> restart(), ex -> {
DiSky.getErrorHandler().exception(e, ex);
restart();
restart();
});
}

event.reply(message.build())
.addActionRows(formatted)
.setEphemeral(hidden)
.queue(v -> restart(), ex -> {
DiSky.getErrorHandler().exception(e, ex);
restart();
});

} else {
Expand Down Expand Up @@ -127,4 +137,4 @@ public void runEffect(Event e, Bot bot) {
return "reply with " + exprMessage.toString(e, debug) + (getChangedVariable() == null ? "" :
" and store the message in " + variableAsString(e, debug));
}
}
}